我有一个 javascript 资源并应用 Rhino 脚本引擎从 java 端调用一个方法。
(function( global ){
var Result;
(Result = function( val ) {
this.tpl = val || '' ;
}).prototype = {
get: function ()
{
return this.tpl ;
}
};
} ( window ) ) ;
这就是我在java端的做法,我如何在上面的脚本中调用get方法?
@Test
public void testCallJSMethod() throws Exception {
String jsResource = getJSResource();
jsEngine.put("window", "window");
jsEngine.put("window.Result", "Result");
jsEngine.eval(jsResource);
Invocable inv = (Invocable) jsEngine;
Object obj = jsEngine.get("window.Result");
assertEquals(true, obj != null);
//try to call get method in above script, but it doesn't work
inv.invokeMethod(obj, "get", new Object[] {} );
}