macos 如何在 OS X 上使用 jsc 运行 JavaScript?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6830192/
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 to run JavaScript using jsc on OS X?
提问by SocialCircus
I got thiswhen I was trying to find some way to run my JavaScript programs through terminal. The run
and load
command mentioned can execute external JavaScript files. Help me how to do this. I am trying to run JavaScript programs which are store locally on my system.
当我试图找到某种方式通过终端运行我的 JavaScript 程序时,我得到了这个。提到的run
andload
命令可以执行外部 JavaScript 文件。帮助我如何做到这一点。我正在尝试运行本地存储在我的系统上的 JavaScript 程序。
EDIT: I am trying to solve Project Euler Q10 in JavaScript. So this is the program that I want to run in NodeJs or JSC. I need help in running the JavaScript files in Node and JSC. Any example will be really helpful.
编辑:我正在尝试用 JavaScript 解决 Project Euler Q10。所以这是我想在 NodeJs 或 JSC 中运行的程序。我需要在 Node 和 JSC 中运行 JavaScript 文件的帮助。任何例子都会非常有帮助。
Thank You all.
谢谢你们。
回答by hurrymaplelad
Complements of this post, JSC lives at
作为这篇文章的补充,JSC 住在
/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc
and is not in the shell PATH
by default. You can fix that with
并且PATH
默认情况下不在 shell 中。你可以用
ln -s /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Resources/jsc /usr/local/bin
jsc
takes filenames as arguments. You could run a file named demo.js
with
jsc
将文件名作为参数。你可以运行一个文件名为demo.js
与
jsc demo.js
Note that you'll have to use debug()
instead of the conventional console.log()
in your script to see any output.
请注意,您必须在脚本中使用debug()
而不是常规console.log()
来查看任何输出。
回答by James Sumners
You're not very clear on what sort of programs these scripts are. Do they do local tasks (e.g. interact with the local file system)? Or are they web scripts that are supposed to interact with (HT|X)ML documents?
您不太清楚这些脚本是什么类型的程序。他们是否执行本地任务(例如与本地文件系统交互)?或者它们是应该与 (HT|X)ML 文档交互的 Web 脚本吗?
If your usage scenario is the former, I recommend using node.js. It is under heavy development, but it is already very usable. For my first project with Node I wrote an XMPP chat bot, completely in JavaScript.
如果你的使用场景是前者,我推荐使用node.js。它正在大力开发中,但它已经非常有用了。在我使用 Node 的第一个项目中,我完全用 JavaScript 编写了一个 XMPP 聊天机器人。
Edit:
编辑:
I seem to have overlooked "using jsc" in the question and answered "How to run JavaScript on OS X?" instead. Still, I think Node is a better alternative if the author is looking to use JavaScript in place of something like Python or Perl.
我似乎忽略了问题中的“使用 jsc”并回答了“如何在 OS X 上运行 JavaScript?” 反而。尽管如此,如果作者希望使用 JavaScript 代替 Python 或 Perl 之类的东西,我认为 Node 是更好的选择。
回答by sherb
NOTE for MacOS Catalina (10.15.x) users
MacOS Catalina (10.15.x) 用户注意事项
jsc
now lives on a new path:
jsc
现在生活在新的道路上:
/System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Helpers/jsc
Make that adjustment to @hurrymaplelad's instructions and you'll be good to go.
对@hurrymaplead 的说明进行调整,您就可以开始了。