简体   繁体   English

我应该使用ES6来转换我的节点js代码吗?

[英]Should I transpile my node js code for using ES6?

I am using nodejs only for server side microservices, I want to use ES6, I have read that Babel is used to transpile code to ES5 to provide support for browsers. 我只使用nodejs服务器端微服务,我想使用ES6,我读过Babel用于将代码转换为ES5以提供对浏览器的支持。 So I need to know if I am not writing this code for client side would I still need to transpile it to run it on V8 engine? 所以我需要知道我是不是在为客户端编写这段代码我是否还需要将其转换为在V8引擎上运行它? Or is it just an extra layer which won't be needed on server side? 或者它只是服务器端不需要的额外层?

You need to use a transpiler if your runtime doesn't support the language features you are using. 如果运行时不支持您正在使用的语言功能,则需要使用转换器。

The latest version of Node.js has almost complete support for ES6 . 最新版本的Node.js 几乎完全支持ES6

If you use features unsupported by whatever version of Node.js you are using, you will need to use a transpiler. 如果您使用的功能不受您使用的任何版本的Node.js支持,则需要使用转换器。

If your server works on the new versions of Node.js and it supports ES6 features, you don't need to transpile your code. 如果您的服务器使用新版本的Node.js并且它支持ES6功能,则无需转换代码。 Your code only works in the same place, if it is hosted in one place. 如果代码托管在一个位置,则代码仅在同一位置工作。

Transpiler is used, when your code is written in newer versions, there may be users which use browsers with old versions of Javascript. 使用Transpiler,当您的代码以较新版本编写时,可能会有用户使用旧版Javascript的浏览器。 So the browser need to get the JS code from the hosted place and run locally, here may be errors. 因此浏览器需要从托管位置获取JS代码并在本地运行,这可能是错误。 In this case you need to use transpiler. 在这种情况下,您需要使用transpiler。

If you are only using the code as a microservice, you don't need to do transpiling. 如果您只是将代码用作微服务,则无需进行转换。 All latest versions of Node.js support es6 by default. 默认情况下,所有最新版本的Node.js都支持es6。 You can refer here for more information of what es6 features are currently supported by Node 您可以在此处参考以获取有关Node当前支持的es6功能的更多信息

Node.js es6 documentation Node.js es6文档

I suggest ues es6/es7 directly in the latest nodejs. 我建议你直接在最新的nodejs中使用es6 / es7。 Now the latest nodejs almost support most functions except import/export. 现在最新的nodejs几乎支持除导入/导出之外的大多数功能。

If so, you can use arrow function, async await etc.. which can make your code more clear. 如果是这样,您可以使用箭头功能,异步等待等...这可以使您的代码更清晰。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM