JavaScript中类似于Ruby的"require"的等效方法是什么?

3
如何在Javascript中将另一个文件“require”到现有文件中?是否有类似于Ruby的“require”或“load”的东西?
注意:我在服务器上使用JS(Rhino)
原因:我只需要访问其他JS文件中的方法。
更新:仅当从cmd行执行时才有效。当我尝试以编程方式调用它时,它会失败。这是我的代码:http://pastie.org/1240495
2个回答

3
要在从Java嵌入的js中使用load函数,必须先在脚本上下文中公开它。可能有一种方法可以从Java中完成,但是也可以使用js进行操作。
免责声明:此解决方案使用了我正在开发的一个受Apache许可证授权的项目中提取的源代码。您可以在这里查看原始源文件。
此js文件设置全局变量,并且保存在名为setupglobals.js的文件中。
var shell = org.mozilla.javascript.tools.shell.Main;
var args = ["-e","var a='STRING';"];
shell.exec(args);

var shellGlobal = shell.global;

//grab functions from shell global and place in current global
load=shellGlobal.load;
print=shellGlobal.print;
defineClass=shellGlobal.defineClass;
deserialize=shellGlobal.deserialize;
doctest=shellGlobal.doctest;
gc=shellGlobal.gc;
help=shellGlobal.help;
loadClass=shellGlobal.loadClass;
quit=shellGlobal.quit;
readFile=shellGlobal.readFile;
readUrl=shellGlobal.readUrl;
runCommand=shellGlobal.runCommand;
seal=shellGlobal.seal;
serialize=shellGlobal.serialize;
spawn=shellGlobal.spawn;
sync=shellGlobal.sync;
toint32=shellGlobal.toint32;
version=shellGlobal.version;
environment=shellGlobal.environment;

这是您原始的Java主机文件,现在已经增加了对setupglobals.js的评估,以便在其他脚本之前运行:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import org.mozilla.javascript.*;

public class RhinoRunner {
    public static void main(String args[]) throws IOException 
    {
        BufferedReader script = new BufferedReader(new FileReader("setupglobals.js"));
        BufferedReader script2 = new BufferedReader(new FileReader("example.js"));
        Context context = Context.enter();
        try {
            ScriptableObject scope = context.initStandardObjects();
            context.evaluateReader(scope, script, "script", 1, null);
            context.evaluateReader(scope, script2, "script2", 1, null);
            Function fct = (Function)scope.get("abc", scope);
            Object result = fct.call(context, scope, scope, new Object[] {2, 3});
            System.out.println(Context.jsToJava(result, int.class));
        } 
        finally 
        {
            Context.exit();
        }
    }
}

这是您的example.js文件,现在已经使用全局加载函数来加载hello.js文件:
function abc(x,y) 
{
    return x+y 
}

load("hello.js")

最后,这里是hello.js:


print("hello world!")

执行时,RhinoRunner会打印以下内容:
hello world!
5

哇!它完美地工作了!非常感谢您! :) 如果您不介意,能否解释一下setupglobals.js的前几行发生了什么? - instantsetsuna
它设置并执行了一个Rhino shell。它有点神奇,但它确实有效。 - jbeard4

2
在Rhino shell中,您应该能够使用预定义的全局方法load()

load([filename, ...])

加载由字符串参数命名的JavaScript源文件。如果给出多个参数,则依次读取并执行每个文件。


不,对我来说不起作用。我遇到了一个错误。这是代码+错误信息:http://pastie.org/1241101 - instantsetsuna
@instantsetsuna:对我有效...其他预定义函数工作吗?比如 print() - Daniel Vassallo
不,print() 也不起作用。我正在使用 Mozilla Rhino 1.7r2,而不是 JDK 自带的那个。也许这是由于版本差异造成的? - instantsetsuna
2
很奇怪。上游犀牛明确地将load()和print()捆绑为其默认环境的一部分。您是如何执行它的?“java -jar js.jar”? - jbeard4
@Echo:我更新了我的问题。从命令行运行很好,但在Java中嵌入后失败了。 - instantsetsuna
@instantsetsuna:请看下面我的回答。 - jbeard4

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接