2 回答

TA貢獻(xiàn)2012條經(jīng)驗(yàn) 獲得超12個(gè)贊
ScriptEngine engine = new ScriptEngineManager().getEngineByName("javascript");
Compilable compilable = (Compilable) engine;
Bindings bindings = engine.createBindings(); // Local級(jí)別的Binding
String script = "var c; function add(op1,op2){ c=1; return c+op1+op2} add(a, b)"; // // 定義函數(shù)并調(diào)用
CompiledScript JSFunction = compilable.compile(script); // 解析編譯腳本函數(shù)
bindings.put("a", 1);
bindings.put("b", 2);
// 通過Bindings加入?yún)?shù)
Object result = JSFunction.eval(bindings);
System.out.println(result); // 調(diào)用緩存著的腳本函數(shù)對(duì)象,Bindings作為參數(shù)容器傳入
out.print(result);
- 2 回答
- 0 關(guān)注
- 672 瀏覽
添加回答
舉報(bào)