【发布时间】:2019-01-27 13:50:28
【问题描述】:
public class Thing {
public Thing() {
}
public void run() {
}
public void out(String s) {
System.out.println(s);
}
}
Context context = Context.create("js");
Value jsBindings = context.getBindings("js");
jsBindings.putMember("this", new Thing());
context.eval("js", "this.run = () => this.out('hi');");
jsBindings.getMember("this").getMember("run").execute();
context.close();
我期待输出 hi 但我没有得到任何输出。
我想知道 javascript(context.eval 部分)是否没有更新我提供的主机对象(new Thing())。
【问题讨论】:
-
你确定
"this"不是Java 中的保留变量名吗? -
@Robert 这很特别,是的。那么如何设置全局
this呢?