我在使用带有 ES6 let 关键字的模块模式(扩充)时遇到错误。
这有效。
var Example = ( Example => {
Example.name = "";
return Example;
})( Example || {} );
console.log(Example);
这不是。
let Example = ( Example => {
Example.name = "";
return Example;
})( Example || {} );
console.log(Example);
我遇到了这个错误。
Uncaught ReferenceError: Example is not defined
})( Example || {} );
^^^^^^^
最佳答案
当你意识到这一点时,答案就变得相当清楚了:
var x = (j => j)(x)
..变成这样:
var x = undefined
x = (j => j)(x)
在计算表达式并将 x
设置为结果之前,它确实将 x
声明为 undefined
。 p>
但是,let
没有那个属性——它没有被提升:
let y = (j => j)(y)
..就这样被评估。
当您执行(j => j)(y)
时,y
不存在,因此会引发引用错误。
关于javascript - ES6 中的模块模式错误(增强),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38679373/