能节点。js和js框架使用ES6开箱即用?

时间:2022-01-29 14:41:05

I read that ES6 was released as and a lot of web browsers support it, I want to now if I can use ES6 features and syntax out of the box in the newest version of node.js and js frameworks like angular.js or react.js ?

我读到ES6是作为发布的,很多web浏览器都支持它,现在我想在最新版本的node中使用ES6的特性和语法。像有棱角的js框架。js或反应。js ?

3 个解决方案

#1


1  

Yes, Node/IO.js can use ES6 out of the box, but support is still limited today. As Felix King noted, you can check the partial support at:

是的,节点/ IO。js可以开箱即用ES6,但目前支持仍然有限。如费利克斯·金所说,你可以在以下网址查看部分支持:

http://kangax.github.io/compat-table/es6

http://kangax.github.io/compat-table/es6

To enable ES6 features in Node.js you can run node with the harmony argument, but be aware that it enables both stable and unstable features:

在节点中启用ES6功能。您可以使用harmony参数来运行节点,但是要注意它能够使稳定和不稳定的特性:

node --harmony app.js

If you use IO.js most stable ES6 features are included by default, however they also have the harmony argument that enables additional staging and in progress features of ES6, so bottom line is that IO.js gives you stable ES6 support out of the box, but you can get additional stuff using the harmony argument.

如果你使用IO。js最稳定的ES6特性是默认包含的,但是它们也有和谐的参数,可以支持额外的阶段和ES6的进度特性,所以底线就是IO。js提供稳定的ES6支持,但您可以使用harmony参数获得其他支持。

iojs --harmony app.js

https://iojs.org/en/es6.html

https://iojs.org/en/es6.html

Since the ES6 support in Node/IO.js is still partial, to get full ES6 support in Node you should use a Transpiler.

因为ES6支持节点/IO。js仍然是部分的,为了在节点中获得完整的ES6支持,您应该使用一个传输器。

For Angular and React, to get ES6 support for the main stuff (classes, arrow funcs etc.) across all browsers you must use a Transpiler, and you have many many options. Some of the most popular are TypeScript, Babel, and Traceur.

对于角度和响应,要在所有浏览器中获得ES6对主要内容(类、箭头功能等)的支持,您必须使用一个传输器,并且您有许多选项。其中最流行的是打字稿、Babel和Traceur。

There are many differences between these, and it depends on what you want. If you want pure ES6/ES7 support, i would go with Babel. If you want optional types and additional features (e.g. Generics) you should go with TypeScript.

它们之间有很多不同之处,这取决于你想要什么。如果你想要纯ES6/ES7的支持,我愿意和Babel一起去。如果您想要可选的类型和附加特性(例如泛型),您应该使用类型文件。

However, Babel has the best tooling today, and i would stick to it for both front-end (Angular/React) and backend (Node).

然而,Babel现在拥有最好的工具,我将在前端(角度/反应)和后端(节点)都使用它。

https://babeljs.io/

https://babeljs.io/

If you are interested to read more about the need for Transpilers read:

如果你有兴趣了解更多关于运输工具的需求,请阅读:

http://www.ministryofprogramming.com/typescript-is-the-new-javascript/

http://www.ministryofprogramming.com/typescript-is-the-new-javascript/

#2


1  

Right now you need to use something to compile your ES6 to ES5. For example this: https://babeljs.io/.

现在您需要使用一些东西来编译ES6到ES5。例如:https://babeljs.io/。

Here is a seed (without docs yet) that uses babeljs.

这里有一个使用babeljs的种子(没有文档)。

https://github.com/ottogiron/node-hapi-seed

https://github.com/ottogiron/node-hapi-seed

With that you can write ES6 and feel like you are using it "out of the box".

有了它,你就可以编写ES6并感觉自己在“开箱即用”地使用它。

#3


0  

Yes! Before we started getting all gitty about Harmony and Babel, CoffeeScript has been spearheading the future for years already. So as a rule of thumb, just ask yourself: can I use CoffeeScript here? The answer, as I said, is most likely yes!

是的!在我们开始对《和谐》和《巴别塔》进行研究之前,CoffeeScript已经引领未来很多年了。作为一个经验法则,你可以问自己:我可以在这里使用CoffeeScript吗?正如我所说,答案很可能是肯定的!

Caveat emptor: depending on the context where your using ES6, you might want to ask yourself what qualifies as being useful at the moment.

购买者请注意:根据您使用ES6的上下文,您可能想问问自己什么是目前可用的。

For instance, React.js has great support for ES6 classes, but unfortunately there is no support for mixins and you'll have to resort to writing higher-order components instead; Node.js has support for generators, but the implementation may not be as mature in terms of performances, etc.

例如,反应。js对ES6类有很好的支持,但不幸的是没有对mixin的支持,因此您不得不转而编写高阶组件;节点。js支持生成器,但是实现在性能等方面可能不够成熟。

#1


1  

Yes, Node/IO.js can use ES6 out of the box, but support is still limited today. As Felix King noted, you can check the partial support at:

是的,节点/ IO。js可以开箱即用ES6,但目前支持仍然有限。如费利克斯·金所说,你可以在以下网址查看部分支持:

http://kangax.github.io/compat-table/es6

http://kangax.github.io/compat-table/es6

To enable ES6 features in Node.js you can run node with the harmony argument, but be aware that it enables both stable and unstable features:

在节点中启用ES6功能。您可以使用harmony参数来运行节点,但是要注意它能够使稳定和不稳定的特性:

node --harmony app.js

If you use IO.js most stable ES6 features are included by default, however they also have the harmony argument that enables additional staging and in progress features of ES6, so bottom line is that IO.js gives you stable ES6 support out of the box, but you can get additional stuff using the harmony argument.

如果你使用IO。js最稳定的ES6特性是默认包含的,但是它们也有和谐的参数,可以支持额外的阶段和ES6的进度特性,所以底线就是IO。js提供稳定的ES6支持,但您可以使用harmony参数获得其他支持。

iojs --harmony app.js

https://iojs.org/en/es6.html

https://iojs.org/en/es6.html

Since the ES6 support in Node/IO.js is still partial, to get full ES6 support in Node you should use a Transpiler.

因为ES6支持节点/IO。js仍然是部分的,为了在节点中获得完整的ES6支持,您应该使用一个传输器。

For Angular and React, to get ES6 support for the main stuff (classes, arrow funcs etc.) across all browsers you must use a Transpiler, and you have many many options. Some of the most popular are TypeScript, Babel, and Traceur.

对于角度和响应,要在所有浏览器中获得ES6对主要内容(类、箭头功能等)的支持,您必须使用一个传输器,并且您有许多选项。其中最流行的是打字稿、Babel和Traceur。

There are many differences between these, and it depends on what you want. If you want pure ES6/ES7 support, i would go with Babel. If you want optional types and additional features (e.g. Generics) you should go with TypeScript.

它们之间有很多不同之处,这取决于你想要什么。如果你想要纯ES6/ES7的支持,我愿意和Babel一起去。如果您想要可选的类型和附加特性(例如泛型),您应该使用类型文件。

However, Babel has the best tooling today, and i would stick to it for both front-end (Angular/React) and backend (Node).

然而,Babel现在拥有最好的工具,我将在前端(角度/反应)和后端(节点)都使用它。

https://babeljs.io/

https://babeljs.io/

If you are interested to read more about the need for Transpilers read:

如果你有兴趣了解更多关于运输工具的需求,请阅读:

http://www.ministryofprogramming.com/typescript-is-the-new-javascript/

http://www.ministryofprogramming.com/typescript-is-the-new-javascript/

#2


1  

Right now you need to use something to compile your ES6 to ES5. For example this: https://babeljs.io/.

现在您需要使用一些东西来编译ES6到ES5。例如:https://babeljs.io/。

Here is a seed (without docs yet) that uses babeljs.

这里有一个使用babeljs的种子(没有文档)。

https://github.com/ottogiron/node-hapi-seed

https://github.com/ottogiron/node-hapi-seed

With that you can write ES6 and feel like you are using it "out of the box".

有了它,你就可以编写ES6并感觉自己在“开箱即用”地使用它。

#3


0  

Yes! Before we started getting all gitty about Harmony and Babel, CoffeeScript has been spearheading the future for years already. So as a rule of thumb, just ask yourself: can I use CoffeeScript here? The answer, as I said, is most likely yes!

是的!在我们开始对《和谐》和《巴别塔》进行研究之前,CoffeeScript已经引领未来很多年了。作为一个经验法则,你可以问自己:我可以在这里使用CoffeeScript吗?正如我所说,答案很可能是肯定的!

Caveat emptor: depending on the context where your using ES6, you might want to ask yourself what qualifies as being useful at the moment.

购买者请注意:根据您使用ES6的上下文,您可能想问问自己什么是目前可用的。

For instance, React.js has great support for ES6 classes, but unfortunately there is no support for mixins and you'll have to resort to writing higher-order components instead; Node.js has support for generators, but the implementation may not be as mature in terms of performances, etc.

例如,反应。js对ES6类有很好的支持,但不幸的是没有对mixin的支持,因此您不得不转而编写高阶组件;节点。js支持生成器,但是实现在性能等方面可能不够成熟。