我在用着Rhino https://www.mozilla.org/rhino/。做完之后
Context cx = Context.enter();
Scriptable scope = cx.initStandardObjects();
cx.evaluateString(scope, "function f(x,y){ return x+y}", "<cmd>", 1, null);
我想知道有一个函数的名称f
和两个参数x
and y
但找不到任何可以帮助我的方法。
尝试使用下一个代码。
package com.qarea.rhinotest;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
public class RhinoTest {
public static void main(String[] args) {
Context cx = Context.enter();
Scriptable scope = cx.initStandardObjects();
cx.evaluateString(scope, "function f(x,y){ return x+y}", "<cmd>", 1, null);
try {
String result = (String) cx.evaluateString(scope, "f.toString()", "<cmd>", 1, null);
System.out.println(result);
} catch (org.mozilla.javascript.EcmaError ex) {
System.out.println(ex.getMessage());
}
}
}
// Maven dependency
// <dependency>
// <groupId>org.mozilla</groupId>
// <artifactId>rhino</artifactId>
// <version>1.7R4</version>
// </dependency>
输出是:
function f(x, y) {
return x + y;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)