javascript 调用 require 时 const 与 let

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28135485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 08:28:38  来源:igfitidea点击:

const vs let when calling require

javascriptnode.jsecmascript-6io.js

提问by Golo Roden

As io.js now supports ES6you are finally able to use the constand letkeywords. Obviously, letis the successor of var, just with some super-powers.

由于 io.js 现在支持 ES6,您终于可以使用constandlet关键字了。很明显,let是继承者var,只是拥有了一些超能力。

But what about const? I know, of course, what "constant" means, but I was wondering when to use it (regarding best practices).

但是呢const?我当然知道“常量”是什么意思,但我想知道何时使用它(关于最佳实践)。

E.g., if I create a module that requires another module, I could write:

例如,如果我创建一个需要另一个模块的模块,我可以这样写:

'use strict';

const util = require('util');

const foo = function () {
  // Do something with util
};

module.exports = foo;

Basically I've replaced every occurence of varwith const. Generally speaking, I think that this is okay, but if I follow this pattern, it leaves me with way more uses of constthan let, as most variables aren't "variables" in a literal sense.

基本上我已经替换了每次出现的varwith const。一般来说,我认为这没问题,但如果我遵循这种模式,它会给我带来const比多得多的用途let,因为大多数变量并不是字面意义上的“变量”。

Is this good style? Should I rather go for let? When should I choose constover let?

这是好风格吗?我应该去let吗?我应该什么时候选择const结束let

采纳答案by thefourtheye

constcan be normally used when you don't want your program

const当你不想要你的程序时可以正常使用

  1. to assign anything to the variable

    "use strict";
    const a = 1;
    a = 2;
    

    will produce TypeError: Assignment to constant variable..

  2. to use the variable without explicitly initializing.

    "use strict";
    const a;
    

    will produce SyntaxError: Unexpected token ;

  1. 给变量赋值

    "use strict";
    const a = 1;
    a = 2;
    

    会产生TypeError: Assignment to constant variable..

  2. 使用变量而不显式初始化。

    "use strict";
    const a;
    

    会产生 SyntaxError: Unexpected token ;

Simply put, I would say,

简单地说,我想说,

  • use constwhenever you want some variables not to be modified

  • use letif you want the exact opposite of const

  • use var, if you want to be compatible with ES5 implementations or if you want module/function level scope.

  • 使用const时,你想要一些变量不被修改

  • 使用let,如果你想的正好相反const

  • 使用var,如果你想与 ES5 实现兼容或者如果你想要模块/函数级别的范围。

Use letonly when you need block level scoping, otherwise using letor varwould not make any difference.

使用let只有当你需要块级别的作用域,以其他方式使用letvar不会作出任何区别。

回答by Gabriel Llamas

I have the same feeling that you're describing. A big percentage of declared variables in my code tend to be constant, even objects and arrays. You can declare constant objects and arrays and still be able to modify them:

我和你描述的感觉一样。我的代码中很大一部分声明的变量往往是常量,甚至是对象和数组。您可以声明常量对象和数组,并且仍然可以修改它们:

const arr = [];
arr.push(1);
arr;
// [ 1 ]

const obj = {};
obj.a = 1;
obj;
// { a: 1 }

AFAIK ES6 modulesdo not require variable declarations when importing, and I suspect that io.js will move to ES6 modules in a near future.

AFAIK ES6 模块在导入时不需要变量声明,我怀疑io.js 将在不久的将来转移到 ES6 模块

I think this is a personal choice. I'd always use const when requiring modules and for module local variables (fooin your example). For the rest of variables, use const appropriately, but never go mad and use const everywhere. I don't know the performance between let and const so I cannot tell if it's better to use const whenever possible.

我认为这是个人选择。在需要模块和模块局部变量(foo在您的示例中)时,我总是使用 const 。对于其余的变量,适当地使用 const,但永远不要发疯并在任何地方使用 const。我不知道 let 和 const 之间的性能,所以我不知道尽可能使用 const 是否更好。

回答by rgwozdz

Performance test constvs letusage on requirefor Node.js 6.10:

Node.js 6.10 的性能测试constlet使用情况require

require('do-you-even-bench')([
  { name: 'test 1', fn: function() { 
    const path = require('path');
    } 
  },
  { name: 'test 2', fn: function() { 
    let path = require('path');
    } 
  }
]);

test 1 .... 2,547,746.72 op/s
test 2 .... 2,570,044.33 op/s

测试 1 .... 2,547,746.72 op/s
测试 2 .... 2,570,044.33 op/s