如何通过终端运行 JavaScript 脚本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8532960/
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
How do you run JavaScript script through the Terminal?
提问by BLUC
For instance, if you were to run a Python script you would type python filename.pyor if you wanted to run a C program make filenamethen ./ filename. How do you do this with .jsfiles?
例如,如果您要运行 Python 脚本,则应键入python filename.py ,或者如果要运行 C 程序make filename则./ filename。你如何用.js文件做到这一点?
采纳答案by Andrzej Doyle
You would need a JavaScript engine (such as Mozilla's Rhino) in order to evaluate the script - exactly as you do for Python, though the latter ships with the standard distribution.
您需要一个 JavaScript 引擎(例如Mozilla 的 Rhino)来评估脚本 - 与您对 Python 所做的完全一样,尽管后者随标准发行版一起提供。
If you have Rhino (or alternative) installed and on your path, then running JS can indeed be as simple as
如果你在你的路径上安装了 Rhino(或替代),那么运行 JS 确实可以像
> rhino filename.js
It's worth noting though that while JavaScript is simply a language in its own right, a lot of particular scripts assume that they'll be executing in a browser-like environment - and so try to access global variables such as location.href
, and create output by appending DOM objects rather than calling print
.
值得注意的是,虽然 JavaScript 本身就是一种语言,但许多特定的脚本假定它们将在类似浏览器的环境中执行 - 因此尝试访问全局变量,例如location.href
, 并通过附加来创建输出DOM 对象而不是调用print
.
If you've got hold of a script which was written for a web page, you may need to wrap or modify it somewhat to allow it to accept arguments from stdin and write to stdout. (I believe Rhino has a mode to emulate standard browser global vars which helps a lot, though I can't find the docs for this now.)
如果您掌握了为网页编写的脚本,则可能需要对其进行一些包装或修改,以允许它接受来自标准输入的参数并写入标准输出。(我相信 Rhino 有一种模拟标准浏览器全局变量的模式,这很有帮助,尽管我现在找不到这方面的文档。)
回答by Ionic? Biz?u
Another answer would be the NodeJS!
另一个答案是 NodeJS!
Node.js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for data-intensive real-time applications that run across distributed devices.
Node.js 是一个基于 Chrome 的 JavaScript 运行时构建的平台,用于轻松构建快速、可扩展的网络应用程序。Node.js 使用事件驱动的非阻塞 I/O 模型,使其轻量级和高效,非常适合跨分布式设备运行的数据密集型实时应用程序。
Using terminal you will be able to start it using node
command.
使用终端,您将能够使用node
命令启动它。
$ node
> 2 + 4
6
>
Note: If you want to exit just type
注意:如果你想退出只需输入
.exit
You can also run a JavaScript file like this:
您还可以像这样运行 JavaScript 文件:
node file.js
回答by microspino
If you have a Macyou can get jsc
a javascript console in OS X (Terminal) by typing
如果您有Mac,则可以jsc
通过键入在 OS X(终端)中获得javascript 控制台
/System/Library/Frameworks/JavaScriptCore.framework/Versions/Current/Resources/jsc
in Terminal.app.
在Terminal.app 中。
You could also run one of your .js script by adding its name as an argument for jsc, like this:
您还可以通过将其名称添加为 jsc 的参数来运行您的 .js 脚本之一,如下所示:
jsc your_awesome_script_name.js
Notice: I use console.log()
during development but jsc needs the debug()
function instead.
注意:我console.log()
在开发过程中使用但 jsc 需要该debug()
功能。
On Ubuntuyou have some nice ECMAScript shellsat your disposal. Between them it's worth to mention SpiderMonkey. You can add It by sudo apt-get install spidermonkey
在Ubuntu 上,您可以使用一些不错的ECMAScript shell。在他们之间,值得一提的是SpiderMonkey。你可以添加它sudo apt-get install spidermonkey
On Windowsas other people said you can rely on cscript
and wscript
directly built on the OS.
在Windows 上,正如其他人所说,您可以依赖cscript
并wscript
直接构建在操作系统上。
I would add also another :) way of thinking to the problem, if you have time and like to learn new things i'd like to mention coffee-scriptthat has its own compiler/console and gives you super-correct Javascript out. You can try it also on your browser (link "try coffeescript").
我还要添加另一种 :)对问题的思考方式,如果您有时间并想学习新事物,我想提及具有自己的编译器/控制台并为您提供超级正确的 Javascript 的咖啡脚本。您也可以在浏览器上尝试(链接“尝试咖啡脚本”)。
回答by Andrey Bodoev
Use node.js for that, here is example how to install node
by using brew
on mac:
为此使用 node.js,这里是如何在 mac 上node
使用安装的示例brew
:
brew update && install node
Then run your program by typing node filename.js
, and you can use console.log()
for output.
然后通过键入运行您的程序node filename.js
,您可以将其console.log()
用于输出。
回答by Mark Lakata
It is crude, but you can open up the Javascript console in Chrome (Ctrl+Shift+J) and paste the text contents of the *.js file and hit Enter.
它很粗糙,但您可以在 Chrome 中打开 Javascript 控制台(Ctrl+Shift+J)并粘贴 *.js 文件的文本内容,然后按 Enter。
回答by Emjey
If you're using MacBook.
如果您使用的是 MacBook。
- Set up
node.js
in your system and open up the terminal - Navigate to the directory, where the js file is saved.
- To execute run
node <filename.js>
node.js
在您的系统中设置并打开终端- 导航到保存 js 文件的目录。
- 执行运行
node <filename.js>
example, if filename is script.js
run node script.js
例如,如果script.js
运行文件名node script.js
回答by fscof
Alternatively, if you're just looking to play around with Javascript a nice in browser option is Codecademy'sJavascript Lab.
或者,如果您只是想使用 Javascript,那么Codecademy 的Javascript 实验室是一个不错的浏览器选项。
They also have a Python and Ruby IDE.
他们还有一个 Python 和 Ruby IDE。
回答by raf
I tried researching that too but instead ended up using jsconsole.comby Remy Sharp (he also created jsbin.com). I'm running on Ubuntu 12.10 so I had to create a special iconbut if you're on Windows and use Chrome simply go to Tools>Create Application Shortcuts (note this doesn't work very well, or at all in my case, on Ubuntu). This site works very like the Mac jsc
console: actually it has some cool features too (like loading libraries/code from a URL) that I guess jsc
does not.
我也尝试过研究,但最终使用了 Remy Sharp 的jsconsole.com(他还创建了 jsbin.com)。我在 Ubuntu 12.10 上运行,所以我必须创建一个特殊的图标,但如果您使用的是 Windows 并使用 Chrome,只需转到工具>创建应用程序快捷方式(注意这不太好用,或者在我的情况下根本不起作用,在 Ubuntu 上)。这个站点的工作方式非常像 Macjsc
控制台:实际上它也有一些很酷的功能(比如从 URL 加载库/代码),我猜jsc
没有。
Hope this helps.
希望这可以帮助。
回答by Anders Tornblad
If you are on a Windows PC, you can use WScript.exe or CScript.exe
如果您使用的是 Windows PC,则可以使用 WScript.exe 或 CScript.exe
Just keep in mind that you are not in a browser environment, so stuff like document.write
or anything that relies on the window
object will not work, like window.alert
. Instead, you can call WScript.Echo
to output stuff to the prompt.
请记住,你是不是在浏览器环境,所以喜欢的东西document.write
或任何依赖上的window
物体将无法正常工作,喜欢window.alert
。相反,您可以调用WScript.Echo
将内容输出到提示。
http://msdn.microsoft.com/en-us/library/9bbdkx3k(VS.85).aspx
http://msdn.microsoft.com/en-us/library/9bbdkx3k(VS.85).aspx
回答by Keith Cascio
On Ubuntu, install libjavascriptcoregtk-3.0-binand use
/usr/bin/jsc
(manpage).
在 Ubuntu 上,安装libjavascriptcoregtk-3.0-bin并使用( manpage)。/usr/bin/jsc