Node.js 支持哪个版本的 Javascript
我正在开始使用 Node.js,但我很难弄清楚 Node 支持哪个版本的 JavaScript,这使得我很难弄清楚我可以使用哪些功能。这是我所知道的。
- Node 使用 V8
- V8 实现 ECMA-262 第 3 版中指定的 ECMAScript
- ECMA-262,第 3 版是 JavaScript 1.5
鉴于此,我假设我可以在 Node.js 中使用 JavaScript 1.5 兼容代码。然而,事实证明我可以使用 Array.forEach
等结构,尽管根据 MDC,它直到 Javascript 1.6 - ECMA-262,第 5 版才可用。
我哪里出错了?是否有文档详细介绍了可用的语言功能?
I'm getting started with Node.js and I'm having a hard time figuring out what version of JavaScript is supported by node which makes it difficult figuring out what features I can use. Here's what I know.
- Node uses V8
- V8 implements ECMAScript as specified in ECMA-262, 3rd edition
- ECMA-262, 3rd edition is JavaScript 1.5
Given this, I would assume I can use JavaScript 1.5 compatible code in node. However, it turns out I can use the Array.forEach
, among other constructs, even though according to MDC it isn't available until Javascript 1.6 -- ECMA-262, 5th edition.
Where am I going wrong? Is there a document somewhere that details the available language features?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这个矩阵(V8 相当接近 WebKit 列)似乎很好地回答了这个问题“我可以使用哪些功能?”但我找不到“支持哪个版本的 javascript?”的规范答案。据我所知,最好的答案是:支持 ECMA-262 第三版,但也支持第五版的许多功能。
此线程<上有一个很好的解释为什么 V8 遵循 WebKit 和 JavaScriptCore 功能< /a>.
This matrix (V8 follows the WebKit column fairly closely) seems to pretty well answer the question "what features can I use?" but I can't find a canonical answer to "what version of javascript is supported?" As far as I can tell, the best answer is this: ECMA-262 3rd edition is supported, but many features of the 5th edition are also supported.
There's a good explanation of why V8 follows the WebKit and JavaScriptCore functionality on this thread.
看起来,在某些时候, node.green 是为了跟踪不同 Node 版本的 JavaScript 功能支持而创建的。
Looks like, at some point, node.green was created to track JavaScript feature support against different Node versions.
Node javascript 版本取决于 Node 使用的 v8 版本。 Node 版本 0.5.1(2011 年 7 月 14 日)及更高版本使用 v8 3.4.10 或更高版本,并且是第 5 版 ECMA-262,而不是第 3 版。第 5 版相当于 Javascript 1.8.5。请参阅以下原因。
2011 年 5 月 21 日至 2011 年 6 月 15 日期间,v8 网站停止将 v8 列为实施第三版 ECMA-262,并开始显示第五版。
http://web.archive.org/web/20110521053630/http ://code.google.com/p/v8/
http://web.archive.org/web/20110615102350/http://code.google.com/p/v8/
根据 v8 变更日志,v8 于 2011 年 6 月 15 日更改为版本 3.4.4。所以该版本及以后的版本是第五版。
根据 Node 变更日志,v8 3.4.10 位于 2011 年 7 月 14 日的 Node Version 0.5.1 中,因此该版本及更高版本是第 5 版 ECMA-26。但这并不意味着 3.4.10 之前的 v8 版本纯粹是第 3 版,因为许多 v8 版本可能从第 3 版到第 5 版稳步过渡。
The Node javascript version depends on which version of v8 that Node uses. Node version 0.5.1 (14 Jul 2011) and upwards use v8 3.4.10 or later, and are 5th edition ECMA-262, rather than 3rd Edition. 5th edition is equivalent to Javascript 1.8.5. See below reasons.
Between 21 May 2011 and 15 June 2011 the v8 website stopped listing v8 as implementing 3rd edition ECMA-262 and started showing 5th edition.
http://web.archive.org/web/20110521053630/http://code.google.com/p/v8/
http://web.archive.org/web/20110615102350/http://code.google.com/p/v8/
According to the v8 changelog, on the 15 June 2011 v8 changed to version 3.4.4. So that version and later are 5th edition.
According to the Node changelog, v8 3.4.10 was in Node Version 0.5.1, 14 Jul 2011, so that version and later are 5th edition ECMA-26. This does not mean that v8 versions before 3.4.10 were purely 3rd edition though, since there may have been a steady transition from 3rd to 5th through many v8 versions.
这是来自 V8 团队的 2016 年博客文章,它解释了为什么这是一团糟,特别是因为2016 年,TC39 开始更频繁地发布新的 JavaScript / ECMAScript 规范。
如果您使用的是 Visual Studio Code(或任何使用语言服务器/LSP的编辑器),则编译目标的有效值包括:例如
es2015
(又名es6
)和es2016
。要知道我的 Node 版本是否支持给定版本的 JavaScript,我能找到的唯一解决方法如下:
node -p process.versions.v8
查找该特定 Node 中的 V8 版本,或通过 Node 文档结果是目前Node LTS(16.13.1) ,包括 V8 9.4,支持所有 ECMAScript 2022 功能。
Here is a 2016 blog post from the V8 team which explains why this is a mess, especially since 2016 when TC39 started releasing new JavaScript / ECMAScript specifications more frequently.
If you are using Visual Studio Code (or any editor using Language Server / LSP) then valid values for the compilation target include such things as
es2015
(akaes6
) andes2016
.The only workaround I can find to know if my version of Node supports a given version of JavaScript goes like this:
node -p process.versions.v8
, or via the Node docsThe result is that currently Node LTS (16.13.1), includes V8 9.4, which supports all the ECMAScript 2022 features.
似乎我们已经被简化为两种策略来确定节点使用哪个版本的 Javascript:
策略 1: 相信某处文档的内容,这在很多情况下是错误的。我还没有找到指示哪个版本的节点支持哪个版本的 ECMAScript 的键值对的表。
策略2:猜测并检查。
找到 ES6 引用的一个功能,然后“看看它是否失败”,类似这样:
我想这里的“babel”是对 Tanakh 的引用。我们的节点版本支持哪些功能?好吧,我不知道,你必须手动测试它。这将变成一个巨大的混乱。更糟糕的是,这似乎是故意的。
It seems as though we have been reduced to two strategies to figure out which version of Javascript node uses:
Strategy 1: trust in what some document somewhere says, which is wrong in many cases. I haven't found the table which indicates the key-value pairs of which version of node supports which version of ECMAScript.
Strategy 2: guess-and-check.
Find a feature quoted by ES6 and "see if it fails", something like this:
I suppose the "babel" here is a reference to the Tanakh. What features does our version of node support? Well, I don't know, you'll have to manually test it. This is going to become a giant MESS. And worse, it seems to be on purpose.