在没有浏览器的情况下执行 JavaScript?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2941411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Executing JavaScript without a browser?
提问by Daniel
I am looking into Javascript programming without a browser. I want to run scripts from the Linux or Mac OS X command line, much like we run any other scripting language (ruby, php, perl, python...)
我正在研究没有浏览器的 Javascript 编程。我想从 Linux 或 Mac OS X 命令行运行脚本,就像我们运行任何其他脚本语言(ruby、php、perl、python...)
$ javascript my_javascript_code.js
I looked into spider monkey (Mozilla) and v8 (Google), but both of these appear to be embedded.
我查看了蜘蛛猴 (Mozilla) 和 v8 (Google),但它们似乎都被嵌入了。
Is anyone using Javascript as a scripting language to be executed from the command line?
是否有人使用 Javascript 作为要从命令行执行的脚本语言?
If anyone is curious why I am looking into this, I've been poking around node.js. The performance of node.js makes me wonder if javascript may be a viable scripting language for processing large data.
如果有人好奇我为什么要研究这个,我一直在探索node.js。node.js 的性能让我怀疑 javascript 是否可能是处理大数据的可行脚本语言。
采纳答案by jasonmp85
I found this related questionon the topic, but if you want direct links, here they are:
我在这个主题上找到了这个相关的问题,但如果你想要直接链接,它们是:
- You can install Rhino as others have pointed out. This postshows an easy way to get it up and running and how to alias a command to invoke it easily
- If you're on a Mac, you can use JavaScriptCore, which invokes WebKit's JavaScript engine. Here's a poston it
- You can use Chome/Google's V8 interpreter as well. Here are instructions
- The JavaScript as OSA is interesting because it lets you (AFAIK) interact with scriptable OS X apps as though you were in AppleScript (without the terrible syntax)
- 您可以像其他人指出的那样安装 Rhino。这篇文章展示了一种启动和运行它的简单方法,以及如何为命令设置别名以轻松调用它
- 如果您使用的是 Mac,则可以使用 JavaScriptCore,它会调用 WebKit 的 JavaScript 引擎。这是一篇关于它的帖子
- 您也可以使用 Chome/Google 的 V8 解释器。这是说明
- 作为 OSA 的 JavaScript 很有趣,因为它让您 (AFAIK) 与可编写脚本的 OS X 应用程序交互,就像您在 AppleScript 中一样(没有糟糕的语法)
I'm surprised node.js doesn't come with a shell, but I guess it's really more like an epoll/selector-based callback/event-oriented webserver, so perhaps it doesn't need the full JS feature set, but I'm not too familiar with its inner workings.
我很惊讶 node.js 没有自带 shell,但我猜它真的更像是一个基于 epoll/selector 的回调/面向事件的 web 服务器,所以也许它不需要完整的 JS 功能集,但我不太熟悉它的内部运作。
Since you seem interested in node.js and since it's based on V8, it might be best to follow those instructions on getting a V8 environment set up so you can have a consistent basis for your JavaScript programming (I should hope JSC and V8 are mostly the same, but I'm not sure).
由于您似乎对 node.js 感兴趣并且因为它基于 V8,因此最好按照有关设置 V8 环境的说明进行操作,以便您可以为 JavaScript 编程提供一致的基础(我希望 JSC 和 V8 主要是一样,但我不确定)。
回答by rbrito
Main Answer
主要答案
Yes, to answer your question, it is possible to use JavaScript as a "regular" scripting language from the command line, without a browser. Since others have not mentioned it yet, I see that it is worth mentioning:
是的,为了回答您的问题,可以从命令行使用 JavaScript 作为“常规”脚本语言,而无需浏览器。由于其他人还没有提到,我看到值得一提的是:
On Debian-based systems (and this includes Ubuntu, Linux Mint, and aptosid/sidux, at least), besides the options of installing Rhino and others already mentioned, you have have other options:
在基于 Debian 的系统上(至少包括 Ubuntu、Linux Mint 和 aptosid/sidux),除了安装 Rhino 和其他已经提到的选项之外,您还有其他选项:
Install the
libmozjs-24-binpackage, which will provide you with Mozilla's Spidermonkey engine on the command line as a simplejs24, which can be used also as an interactive interpreter. (The24in the name means that it corresponds to version 24 of Firefox).Install the
libv8-devpackage, which will provide you Google's V8 engine. It has, as one of its examples, the file/usr/share/doc/libv8-dev/examples/shell.cc.gzwhich you can uncompress and compile very simply (e.g.,g++ -Os shell.cc -o shell -lv8).Install the package
nodejsand it will be available both as the executablenodejsand as an alternative(in the Debian-sense) to provide thejsexecutable. JIT compilation is provided as a courtesy of V8.Install the package
libjavascriptcoregtk-3.0-binand use WebKit's JavaScriptCore interpreter (jsc) as a regular interpreter from the command-line. And this is without needing to have access to a Mac. On many platforms (e.g., x86 and x86_64), this interpreter will come with a JIT compiler.
安装
libmozjs-24-bin包,它将在命令行上为您提供 Mozilla 的 Spidermonkey 引擎作为一个简单的js24,它也可以用作交互式解释器。(24名称中的 表示它对应于 Firefox 24 版)。安装该
libv8-dev软件包,它将为您提供 Google 的 V8 引擎。作为它的示例之一,它具有/usr/share/doc/libv8-dev/examples/shell.cc.gz可以非常简单地解压缩和编译的文件(例如,g++ -Os shell.cc -o shell -lv8)。安装该软件包
nodejs,它将作为可执行文件nodejs和替代(在 Debian 意义上)提供js可执行文件。JIT 编译是由 V8 提供的。安装包
libjavascriptcoregtk-3.0-bin并jsc从命令行使用 WebKit 的 JavaScriptCore 解释器 ( ) 作为常规解释器。这是不需要访问 Mac 的。在许多平台上(例如,x86 和 x86_64),这个解释器会带有一个 JIT 编译器。
So, with almost no compilation you will have three of the heavy-weight JavaScript engines at your disposal.
因此,几乎不需要编译,您就可以使用三个重量级的 JavaScript 引擎。
Addendum
附录
Once you have things installed, you can simply create files with the #!/usr/bin/jsshebang line and things will just work:
一旦你安装了东西,你可以简单地使用#!/usr/bin/jsshebang 行创建文件,事情就会正常工作:
$ cat foo.js
#!/usr/bin/js
console.log("Hello, world!");
$ ls -lAF /usr/bin/js /etc/alternatives/js /usr/bin/nodejs
lrwxrwxrwx 1 root root 15 Jul 16 04:26 /etc/alternatives/js -> /usr/bin/nodejs*
lrwxrwxrwx 1 root root 20 Jul 16 04:26 /usr/bin/js -> /etc/alternatives/js*
-rwxr-xr-x 1 root root 1422004 Apr 28 20:31 /usr/bin/nodejs*
$ chmod a+x foo.js
$ ./foo.js
Hello, world!
$ js ./foo.js
Hello, world!
$
回答by nonopolarity
I have installed Node.js on an iMac and
我已经在 iMac 上安装了 Node.js 并且
node somefile.js
in bash will work.
在 bash 中会起作用。
回答by Cheeso
I know you asked about Linux and Mac; I am going to provide the answer for Windows, in case other people who are interested in Windows find your question .
我知道你问过 Linux 和 Mac;我将提供 Windows 的答案,以防其他对 Windows 感兴趣的人找到您的问题。
Windows includes a Javascript engine that can be used from the command line.
Windows 包含一个可以从命令行使用的 Javascript 引擎。
All versions of Windows, since Windows 98, have included something called "The Windows Script Host". It's a windows-standard way to support script "engines". Since the first release, WSH supports JScript, Microsoft's version of Javascript. Among other things, this means that, from a windows command line, you can just invoke the name of any *.js file, and it will run in the JScript engine. (via either wscript.exe or cscript.exe)
自 Windows 98 以来,所有版本的 Windows 都包含称为“ Windows 脚本宿主”的内容。这是支持脚本“引擎”的 Windows 标准方式。从第一个版本开始,WSH 就支持 JScript,Microsoft 的 Javascript 版本。这意味着,从 Windows 命令行,您可以调用任何 *.js 文件的名称,并且它将在 JScript 引擎中运行。(通过 wscript.exe 或 cscript.exe)
You can see this question: What is the ProgId or CLSID for IE9's Javascript engine (code-named "Chakra")to learn how to invoke the higher-performance IE9 Javascript engine from cscript.exe.
您可以看到这个问题: IE9 的 Javascript 引擎(代号为“Chakra”)的 ProgId 或 CLSID 是什么,以了解如何从 cscript.exe 调用更高性能的 IE9 Javascript 引擎。
回答by jbandi
Since nobody mentioned it: Since Java 1.6 The Java JDK also comes bundled with a JavaScript commandline and REPL.
因为没有人提到它:从 Java 1.6 开始,Java JDK 还捆绑了 JavaScript 命令行和 REPL。
It is based on Rhino: https://developer.mozilla.org/en/docs/Rhino
它基于 Rhino:https: //developer.mozilla.org/en/docs/Rhino
In Java 1.6 and 1.7 the command is called jrunscript(jrunscript.exeon Windows) and can be found in the bin folder of the JDK.
在 Java 1.6 和 1.7 中,该命令被调用jrunscript(jrunscript.exe在 Windows 上)并且可以在 JDK 的 bin 文件夹中找到。
Starting from Java 1.8 there is bundled a new JavaScript implementation (Nashorn: https://blogs.oracle.com/nashorn/)
从 Java 1.8 开始,捆绑了一个新的 JavaScript 实现(Nashorn:https://blogs.oracle.com/nashorn/ )
So in Java 1.8 the command is called jjs(jjs.exeon Windows)
所以在 Java 1.8 中调用命令jjs(jjs.exe在 Windows 上)
回答by thejefflarson
FWIW, node.js comes with a shell, try typing in:
FWIW,node.js 带有一个 shell,尝试输入:
node-repl
once you've installed node.js to see it in action. It's pretty standard to install rlwrap to get it to work nicely.
一旦你安装了 node.js 以查看它的运行情况。安装 rlwrap 以使其正常工作是非常标准的。
回答by neebz
回答by Daniel Vassallo
回答by Eric Leschinski
I use Ubuntu 12.10 and js from commandline
我从命令行使用 Ubuntu 12.10 和 js
It is available with my installation of java:
它在我安装的 java 中可用:
el@apollo:~/foo$ java -version
java version "1.6.0_27"
el@apollo:~/foo$ which js
/usr/bin/js
Some examples:
一些例子:
el@apollo:~/foo$ js
> 5
5
> console.log("hello");
hello
undefined
> var f = function(){ console.log("derp"); };
undefined
> f();
derp
> var mybool = new Boolean();
undefined
> mybool
{}
> mybool == true
false
> mybool == false
true
> var myobj = {};
undefined
> myobj.skipper = "on my mark, engage!"
'on my mark, engage!'
> myobj.skipper.split(" ");
[ 'on',
'my',
'mark,',
'engage!' ]
The sky is the limit, then keep right on going.
天空是极限,然后继续前进。

