JavaScript(Rhino)使用库或包含其他脚本

时间:2022-12-06 09:16:20

In JDK6, is there a way to load multiple scripts, each in a file, and have the one script reference a method of another script? Sort of like "include"?

在JDK6中,有没有办法加载多个脚本,每个脚本都在一个文件中,并让一个脚本引用另一个脚本的方法?有点像“包括”?

4 个解决方案

#1


27  

I think you're after the load() method/property of Rhino's global object/scope

我想你是在Rhino的全局对象/范围的load()方法/属性之后

load("file1.js");
load("file2.js");
load("file3.js");

methodFromFileOne();
var bar = methodFromFileTwo();
var etc = dotDotDot();

This will load a javascript source file, similar to how include/require will in PHP. Once you load a file, you'll be able to call and function or use any object defined in the loaded file.

这将加载一个javascript源文件,类似于PHP中的include / require方式。加载文件后,您将能够调用并运行或使用加载文件中定义的任何对象。

This is how things work when you're using the Rhino shell, which is the only context I know (your question mentioned the Java SDK, which is outside my area of experience)

这是你使用Rhino shell时的工作方式,这是我所知道的唯一上下文(你的问题提到了Java SDK,这超出了我的经验范围)

#2


10  

if you happen to be trying to do this within ant, you might see this error:

如果你碰巧在ant中尝试这样做,你可能会看到这个错误:

<script language="javascript">
    load('foo.js');
</script>
javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: TypeError: Cannot find function load.

but you can sidestep it:

但你可以回避它:

<script language="javascript">
    eval(''+new String(org.apache.tools.ant.util.FileUtils.readFully(new java.io.FileReader('foo.js'))));
</script>

#3


10  

A real-life example this time, i.e. running the esprima parser with Rhino 1.7R4.

这次是一个真实的例子,即使用Rhino 1.7R4运行esprima解析器。

import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
...

Context context = Context.enter();
Scriptable globalScope = context.initStandardObjects();
Reader esprimaLibReader = new InputStreamReader(getClass().getResourceAsStream("/esprima.js"));
context.evaluateReader(globalScope, esprimaLibReader, "esprima.js", 1, null);

// Add a global variable out that is a JavaScript reflection of the System.out variable:
Object wrappedOut = Context.javaToJS(System.out, globalScope);
ScriptableObject.putProperty(globalScope, "out", wrappedOut);

String code = "var syntax = esprima.parse('42');" +
    "out.print(JSON.stringify(syntax, null, 2));";

// The module esprima is available as a global object due to the same
// scope object passed for evaluation:
context.evaluateString(globalScope, code, "<mem>", 1, null);
Context.exit();

After running this code, you should see the output as follows:

运行此代码后,您应该看到输出如下:

{
  "type": "Program",
  "body": [
    {
      "type": "ExpressionStatement",
      "expression": {
        "type": "Literal",
        "value": 42,
        "raw": "42"
      }
    }
  ]
}

So indeed, the trick is in reusing the globalScope object.

事实上,诀窍在于重用globalScope对象。

#4


6  

As long as you use the same scope to execute each file, they will be able to reference functions and variables from previously executed files.

只要您使用相同的范围来执行每个文件,它们就能够从以前执行的文件中引用函数和变量。

#1


27  

I think you're after the load() method/property of Rhino's global object/scope

我想你是在Rhino的全局对象/范围的load()方法/属性之后

load("file1.js");
load("file2.js");
load("file3.js");

methodFromFileOne();
var bar = methodFromFileTwo();
var etc = dotDotDot();

This will load a javascript source file, similar to how include/require will in PHP. Once you load a file, you'll be able to call and function or use any object defined in the loaded file.

这将加载一个javascript源文件,类似于PHP中的include / require方式。加载文件后,您将能够调用并运行或使用加载文件中定义的任何对象。

This is how things work when you're using the Rhino shell, which is the only context I know (your question mentioned the Java SDK, which is outside my area of experience)

这是你使用Rhino shell时的工作方式,这是我所知道的唯一上下文(你的问题提到了Java SDK,这超出了我的经验范围)

#2


10  

if you happen to be trying to do this within ant, you might see this error:

如果你碰巧在ant中尝试这样做,你可能会看到这个错误:

<script language="javascript">
    load('foo.js');
</script>
javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError: TypeError: Cannot find function load.

but you can sidestep it:

但你可以回避它:

<script language="javascript">
    eval(''+new String(org.apache.tools.ant.util.FileUtils.readFully(new java.io.FileReader('foo.js'))));
</script>

#3


10  

A real-life example this time, i.e. running the esprima parser with Rhino 1.7R4.

这次是一个真实的例子,即使用Rhino 1.7R4运行esprima解析器。

import org.mozilla.javascript.Context;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;
...

Context context = Context.enter();
Scriptable globalScope = context.initStandardObjects();
Reader esprimaLibReader = new InputStreamReader(getClass().getResourceAsStream("/esprima.js"));
context.evaluateReader(globalScope, esprimaLibReader, "esprima.js", 1, null);

// Add a global variable out that is a JavaScript reflection of the System.out variable:
Object wrappedOut = Context.javaToJS(System.out, globalScope);
ScriptableObject.putProperty(globalScope, "out", wrappedOut);

String code = "var syntax = esprima.parse('42');" +
    "out.print(JSON.stringify(syntax, null, 2));";

// The module esprima is available as a global object due to the same
// scope object passed for evaluation:
context.evaluateString(globalScope, code, "<mem>", 1, null);
Context.exit();

After running this code, you should see the output as follows:

运行此代码后,您应该看到输出如下:

{
  "type": "Program",
  "body": [
    {
      "type": "ExpressionStatement",
      "expression": {
        "type": "Literal",
        "value": 42,
        "raw": "42"
      }
    }
  ]
}

So indeed, the trick is in reusing the globalScope object.

事实上,诀窍在于重用globalScope对象。

#4


6  

As long as you use the same scope to execute each file, they will be able to reference functions and variables from previously executed files.

只要您使用相同的范围来执行每个文件,它们就能够从以前执行的文件中引用函数和变量。