让 let 替换匿名闭包以获得私有变量吗?
Does let replace anonymous closures for having private variables?
如果你想在 JavaScript 中有某种私有变量,你可以将代码放在匿名闭包中,对吧?现在既然包含了 let
,闭包的这个特定用例就消失了吗?还是它仍然相关?
顶级示例:
// global variable a
var a = 6;
// using let
{
// new variable a that only lives inside this block
let a = 5;
console.log(a); // => 5
}
console.log(a); // => 6
// using a closure
(function() {
// again a new variable a that only lives in this closure
var a = 3;
console.log(a); // => 3
})();
console.log(a); // => 6
在Javascript中有一个叫做Hoisting的东西,"hoists"上面的变量甚至在初始化之前。
// global variable a
var a = 6;
// using let
{
// new variable a that only lives inside this block
let a = 5;
console.log(a); // => 5
}
console.log(a); // => 6
// using a closure
(function() {
// again a new variable a that only lives in this closure
var a = 3;
console.log(a); // => 3
})();
console.log(a); // => 6
因此此代码更改为:
// The global variable 'a' is hoisted on the top of current scope which is Window as of now
var a;
// Initialization takes place as usual
a = 6;
// This is a block
{
// This local variable 'a' is hoisted on the top of the current scope which is the 'block'
let a;
a = 5;
console.log(a); // => 5
}
console.log(a); // => 6
// Using an IIFE
(function() {
// This local variable 'a' is hoisted on the top of the current scope which is the block of IIFE
var a;
a = 3;
console.log(a); // => 3
})();
console.log(a); // => 6
Pre ES6 我们曾经使用 IIFEs to make variables which would not pollute the global scope, but after ES6 we generally use let
and const
because they provide block-scope.
如果你想在 JavaScript 中有某种私有变量,你可以将代码放在匿名闭包中,对吧?现在既然包含了 let
,闭包的这个特定用例就消失了吗?还是它仍然相关?
顶级示例:
// global variable a
var a = 6;
// using let
{
// new variable a that only lives inside this block
let a = 5;
console.log(a); // => 5
}
console.log(a); // => 6
// using a closure
(function() {
// again a new variable a that only lives in this closure
var a = 3;
console.log(a); // => 3
})();
console.log(a); // => 6
在Javascript中有一个叫做Hoisting的东西,"hoists"上面的变量甚至在初始化之前。
// global variable a
var a = 6;
// using let
{
// new variable a that only lives inside this block
let a = 5;
console.log(a); // => 5
}
console.log(a); // => 6
// using a closure
(function() {
// again a new variable a that only lives in this closure
var a = 3;
console.log(a); // => 3
})();
console.log(a); // => 6
因此此代码更改为:
// The global variable 'a' is hoisted on the top of current scope which is Window as of now
var a;
// Initialization takes place as usual
a = 6;
// This is a block
{
// This local variable 'a' is hoisted on the top of the current scope which is the 'block'
let a;
a = 5;
console.log(a); // => 5
}
console.log(a); // => 6
// Using an IIFE
(function() {
// This local variable 'a' is hoisted on the top of the current scope which is the block of IIFE
var a;
a = 3;
console.log(a); // => 3
})();
console.log(a); // => 6
Pre ES6 我们曾经使用 IIFEs to make variables which would not pollute the global scope, but after ES6 we generally use let
and const
because they provide block-scope.