(function b() {
console.log(b);
b = 20;
console.log(b);
})();
我写了一个javascript iife 函数。 第一个console.log是函数体。 然后创建b变量,值为20。 第二个console.log也是函数体。 为什么不是20个?
因为
b
是常数,无法赋值。您处于非严格模式,因此分配只是默默地执行任何操作,但如果您使用严格模式,您将收到错误。
(function b() {
'use strict';
console.log(b);
b = 20; // Throws an exception
console.log(b); // Never gets to here
})();