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
const vs let when calling require
提问by Golo Roden
As io.js now supports ES6you are finally able to use the const
and let
keywords. Obviously, let
is the successor of var
, just with some super-powers.
由于 io.js 现在支持 ES6,您终于可以使用const
andlet
关键字了。很明显,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 var
with const
. Generally speaking, I think that this is okay, but if I follow this pattern, it leaves me with way more uses of const
than let
, as most variables aren't "variables" in a literal sense.
基本上我已经替换了每次出现的var
with const
。一般来说,我认为这没问题,但如果我遵循这种模式,它会给我带来const
比多得多的用途let
,因为大多数变量并不是字面意义上的“变量”。
Is this good style? Should I rather go for let
? When should I choose const
over let
?
这是好风格吗?我应该去let
吗?我应该什么时候选择const
结束let
?
采纳答案by thefourtheye
const
can be normally used when you don't want your program
const
当你不想要你的程序时可以正常使用
to assign anything to the variable
"use strict"; const a = 1; a = 2;
will produce
TypeError: Assignment to constant variable.
.to use the variable without explicitly initializing.
"use strict"; const a;
will produce
SyntaxError: Unexpected token ;
给变量赋值
"use strict"; const a = 1; a = 2;
会产生
TypeError: Assignment to constant variable.
.使用变量而不显式初始化。
"use strict"; const a;
会产生
SyntaxError: Unexpected token ;
Simply put, I would say,
简单地说,我想说,
use
const
whenever you want some variables not to be modifieduse
let
if you want the exact opposite ofconst
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 let
only when you need block level scoping, otherwise using let
or var
would not make any difference.
使用let
只有当你需要块级别的作用域,以其他方式使用let
或var
不会作出任何区别。
回答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 (foo
in 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 const
vs let
usage on require
for Node.js 6.10:
Node.js 6.10 的性能测试const
与let
使用情况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