이 샘플 몇 가지 작업을 수행의 처리 기능 속성에
감사를 얻을 수있는 오브젝트 트리를 통과 않습니다. 예를 들어 sampleProcessor
을 꺼내고 process
속성을 꺼내서 해당 기능을 실행합니다.
또한 예제에있는 System.out
개체를 사용할 수 있도록 범위에 Java 개체를 추가하는 방법을 보여줍니다.
package grimbo.test.rhino;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Function;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
public class InvokeFunction {
public static void main(String[] args) {
String sb = "var sampleProcessor = {\n" + " process: function(data){\n out.println(0); return 1+1;\n }\n" + "}";
Context cx = Context.enter();
Scriptable scope = cx.initStandardObjects();
Object out = Context.javaToJS(System.out, scope);
ScriptableObject.putProperty(scope, "out", out);
cx.evaluateString(scope, sb.toString(), "Test", 1, null);
// get the sampleProcessor object as a Scriptable
Scriptable processor = (Scriptable) scope.get("sampleProcessor", scope);
System.out.println(processor);
// get the process function as a Function object
Function processFunction = (Function) processor.get("process", processor);
System.out.println(processFunction);
// execute the process function
Object ob = cx.evaluateString(scope, "sampleProcessor.process()", "Execute process", 1, null);
System.out.println(ob);
}
}
출력 :
[object Object]
[email protected]
0.0
2
안녕 바울은 도움을 많이 감사합니다, 당신의 예는 정확히 내가 무엇을 찾고 있었다입니다 – yser77