如何使用 Apache Rhino 调用子属性函数
如果我有一个如下所示的 js 对象存储在 js 文件中,
var _sampleProcessor = {
process: function(data){
...
}
}
我将如何使用 Apache Rhino 来调用 process 函数?
// sb holds the contents of the js file
Context cx = Context.enter();
Scriptable scope = cx.initStandardObjects();
cx.evaluateString(scope, sb.toString(), "Test", 1, null);
Object processor = scope.get("sampleProcessor ", scope);
if (processor == Scriptable.NOT_FOUND) {
System.out.println("processor is not defined.");
}
到达对象的根很容易,但是我如何遍历对象树以获取过程函数属性
提前致谢
If i have a js object such as below stored in a js file
var _sampleProcessor = {
process: function(data){
...
}
}
How would i use Apache Rhino to call the process function?
// sb holds the contents of the js file
Context cx = Context.enter();
Scriptable scope = cx.initStandardObjects();
cx.evaluateString(scope, sb.toString(), "Test", 1, null);
Object processor = scope.get("sampleProcessor ", scope);
if (processor == Scriptable.NOT_FOUND) {
System.out.println("processor is not defined.");
}
Getting to the root of the object is easy, but how do i traverse the object tree to get at the process function property
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该示例做了一些事情。像您的示例一样提取
sampleProcessor
,并且还提取process
属性并执行该函数。它还展示了将 Java 对象添加到作用域中以便可以使用它们 - 示例中的 System.out 对象。
输出:
This sample does a few things. Pulls out the
sampleProcessor
as your example does, and also pulls out theprocess
property and executes that function.It also shows adding Java objects into the scope so they can be used - the
System.out
object in the example.Output: