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

标签 java javascript function rhino

我正在使用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/

相关文章:

Java:使用具有通配符类型参数的类函数

java - 在 Java 中使用分隔符(与拆分相反)连接数组元素的快速简便方法

java - protected /公开的内部类

javascript - 如何使用 JavaScript 将格式应用于 HTML 表格中的 <td>?

javascript - Angular Strap Datepicker : Inconsistent timestamps returned, UTC +0 vs UTC +12 小时

java - Netty:如何将查询字符串与 websocket 一起使用?

javascript - 没有匹配时跳出 ReactRouter

javascript - 使用参数重命名函数而不重写它们 - Javascript

Matlab 对于类型 'C' 的输入参数未定义函数或方法 'double'

function - 函数什么时候太长?