简体   繁体   English

如何在 OS X 上安装 Javascript 运行时解释器?

[英]How do I get a Javascript runtime interpreter installed on OS X?

With OS X Xcode installed one can run ruby code in terminal with ">ruby somefile.rb" command (or from inside TextWrangler with the run command).安装 OS X Xcode 后,可以在终端中使用“>ruby somefile.rb”命令(或从 TextWrangler 中使用运行命令)运行 ruby 代码。 You can also run a ruby interpreter from in terminal and type code in single lines while the interpreter retains variable objects.您还可以从终端运行 ruby 解释器并在单行中键入代码,同时解释器保留变量对象。

Want same possibilities for Javascript. What must I install (if anything) on OS X to have that functionality?想要 Javascript 的相同可能性。我必须在 OS X 上安装什么(如果有的话)才能具有该功能? Not looking for an IDE as such just an interpreter that will run in Terminal.app?不是在寻找 IDE 本身,只是一个将在 Terminal.app 中运行的解释器?

I assume this is an allowed question for Stackoverflow based on other allowed questions about IDEs for various languages like: What's a good IDE for Python on Mac OS X?我假设这是 Stackoverflow 的允许问题,基于其他关于各种语言的 IDE 的允许问题,例如: What's a good IDE for Python on Mac OS X?

There is a Javascript interpreter in the JavaScriptCore framework that comes with OS X. The interpreter is called jsc and can be found at the following path: OS X自带的JavaScriptCore框架中有一个Javascript解释器,这个解释器叫做jsc,可以在以下路径找到:

 /System/Library/Frameworks/JavaScriptCore.framework/Versions/Current/Helpers/jsc

There is a built-in function, quit() , which will exit interactive mode.有一个内置的 function, quit() ,它会退出交互模式。

If you want to make it easier to use I suggest creating a symlink to a location in your path, eg:如果你想让它更容易使用,我建议创建一个指向路径中某个位置的符号链接,例如:

sudo ln -s /System/Library/Frameworks/JavaScriptCore.framework/Versions/Current/Helpers/jsc /usr/local/bin

This will put a symbolic link in /usr/local/bin .这将在/usr/local/bin中放置一个符号链接。

Node.js . Node.js It's the V8 engine + libraries + REPL.它是 V8 引擎 + 库 + REPL。

Although Node is usually used for web.network-related applications, at its core it's just a plain JS engine and can even be used for shell scripting.尽管 Node 通常用于与 web.network 相关的应用程序,但其核心只是一个普通的 JS 引擎,甚至可以用于 shell 脚本。

You can install it from the installer , brew or just ./configure && make from a Node.js' tarball.您可以从 installerbrew./configure && make从 Node.js 的 tarball 安装它。

There's also Rhino .还有犀牛

v8. v8. Its the javascript engine used in google chrome.它是谷歌浏览器中使用的 javascript 引擎。 You have to compile it for mac OS X, though.不过,您必须为 mac OS X 编译它。 Theres a good tutorial here .这里有一个很好的教程。

You have two options:你有两个选择:

  1. Use console in your Browser: Browsers such as chrome,safari, firefox come buildtin with console which are cappable of running javascript. to open open console on chrome.在浏览器中使用控制台:chrome、safari、firefox 等浏览器自带控制台,可以运行 javascript。以在 chrome 上打开打开的控制台。 Press CTRL + SHIFT + JCTRL + SHIFT + J

  2. Install Nodejs: As others have pointed out, try http://nodejs.org/ using which you can run javascript in terminal app similar to irb安装 Nodejs:正如其他人所指出的,尝试http://nodejs.org/使用它你可以在类似于 irb 的终端应用程序中运行 javascript

On macOS Big Sur you can easily access you OS JavaScript interpreter by adding your local user binary folder.在 macOS Big Sur 上,您可以通过添加本地用户二进制文件夹轻松访问 OS JavaScript 解释器。 You can create a new symlink in /usr/local/bin with您可以在/usr/local/bin中创建一个新的符号链接

% ln -s /System/Library/Frameworks/JavaScriptCore.framework/Versions/A/Helpers/jsc /usr/local/bin/

Then you can access the OS's JavaScript interpreter by just typing然后您只需键入即可访问操作系统的 JavaScript 解释器

% jsc

Hint: Do not forget, that there is no console object, you can use debug(...) instead提示:不要忘记,没有console object,您可以使用debug(...)

On macOS 10.13 High Sierra the location for the jsc binary is:在 macOS 10.13 High Sierra上, jsc二进制文件的位置是:

/System/Library/Frameworks/JavaScriptCore.framework/Versions/Current/Resources/jsc

Using @mttrb's answer simply replace the path if you're on High Sierra.如果您在 High Sierra 上,使用@mttrb 的答案只需替换路径即可。 Seems like this binary has moved around a lot during macOS versions, so you would have to look around under the /System/Library/Frameworks/JavaScriptCore.framework/... path to find the location of jsc .似乎这个二进制文件在 macOS 版本中移动了很多,所以你必须在/System/Library/Frameworks/JavaScriptCore.framework/...路径下四处寻找jsc的位置。

About documentation of jsc关于jsc的文档

The JavaScriptCore Swift/Objective-C framework of is a part of the Apple JavaScript additions for mac apps but the Apple Docs do very little to support the jsc binary. JavaScriptCore Swift/Objective-C框架是 Apple JavaScript 为 mac 应用程序添加的一部分,但 Apple Docs 对jsc二进制文件的支持很少。 However, the original webkit docs do more:然而,原始的webkit文档做的更多:

https://trac.webkit.org/wiki/JSC https://trac.webkit.org/wiki/JSC

Here you will find the functions mentioned in other answers here, like the debug() (replaces console.log() ), the quit() (that exits terminal interactive shell mode) and so on.在这里你会找到其他答案中提到的功能,比如debug() (取代console.log() ), quit() (退出终端交互 shell 模式)等等。

Other JavaScript shells其他JavaScript弹

Here is a list of a couple of JS shells , many of which are cross-platform and can be installed using a single pre-compiled binary (such as JSDB):这是几个 JS shell 的列表,其中许多是跨平台的,可以使用单个预编译的二进制文件(例如 JSDB)进行安装:

https://reference.codeproject.com/javascript/shells https://reference.codeproject.com/javascript/shells

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Windows Runtime JavaScript App中获取操作系统和设备信息? - How get os and device info in Windows Runtime JavaScript App? 如何检测是否使用JavaScript安装了quicktime? - How do I detect if quicktime is installed with javascript? 如何检查Firebug是否安装了javascript? - How do I check if firebug is installed with javascript? 如何保存由OS X仪表板小部件生成的文件? - How do I save a file generated by an OS X Dashboard Widget? 如何在 Yosemite OS X 的 Safari 8 中复制到剪贴板? - How do I copy to clipboard in Safari 8 on Yosemite OS X? 如何使用JavaScript检查是否安装了GEARS? - How do I check if GEARS is installed, using javascript? 无法在RedHat中安装/识别Javascript运行时(对于Rails 3.1) - Can't Get a Javascript Runtime Installed/Recognized in RedHat (for Rails 3.1) 如何使用 javascript 获取已安装的 chrome 扩展列表 - How can I get installed chrome extensions list with javascript 从 Sentry Javascript 显示/获取运行时、操作系统、设备上下文 - Show/Get runtime, os, device contexts from Sentry Javascript 如何在运行时将JavaScript警报添加到innerHTML - How do I add a javascript alert to innerHTML at runtime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM