gpt4 book ai didi

java - 使用 Rhino 从评估的 JS 中获取函数名称及其参数

转载 作者:行者123 更新时间:2023-12-02 08:52:10 25 4
gpt4 key购买 nike

我正在使用Rhino 。做完之后

Context cx = Context.enter();
Scriptable scope = cx.initStandardObjects();
cx.evaluateString(scope, "function f(x,y){ return x+y}", "<cmd>", 1, null);

我想知道有一个名为 f 和两个参数 xy 的函数但找不到任何可以帮助我的方法。

最佳答案

尝试使用下一个代码。

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;
}

关于java - 使用 Rhino 从评估的 JS 中获取函数名称及其参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11515710/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com