简体   繁体   English

我应该在节点 js 中使用 var 还是使用 es6 约定

[英]should i be using var in node js or does it use es6 convention

im new to node and ive been using let and const mostly for declaring variables, but in searching through documentation im reading a lot of references to using var as a variable instead of let, does nodejs use the same variable scoping as vanilla js and the reference documents im reading just outdated?我是 node 新手,我一直在使用 let 和 const 主要用于声明变量,但是在搜索文档时,我阅读了很多关于使用 var 作为变量而不是 let 的参考,nodejs 是否使用与 vanilla js 和参考相同的变量范围我正在阅读的文件刚刚过时?

Yes, both Node and browser run JavaScript in the same fashion, they both adhere to EcmaScript, which specifies the rules of JavaScript.是的,Node 和浏览器都以相同的方式运行 JavaScript,它们都遵守 EcmaScript,它指定了 JavaScript 的规则。

There are ways to know exactly which ES6 features are supported, but NodeJS has been supporting let and const since a long time.有一些方法可以确切地知道支持哪些 ES6 特性,但是 NodeJS 长期以来一直支持letconst The only disparity you should probably worry about is ESM vs CommonJS but it doesn't have anything to do with var/let/const.您可能应该担心的唯一差异是 ESM 与 CommonJS,但它与 var/let/const 没有任何关系。

So I'd encourage you to use let and const instead of var when writing NodeJS code.所以我鼓励你在编写 NodeJS 代码时使用letconst而不是var

You can use either, Node.js supports both let and const since version 6.0.0, see您可以使用任何一种,Node.js 从版本 6.0.0 开始支持 let 和 const,请参阅

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let . https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let

I would recommend using block scoped statements: let and const , since this is more consistent with other languages.我建议使用块范围语句: letconst ,因为这与其他语言更一致。

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

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