gpt4 book ai didi

javascript - ES6 中的模块模式错误(增强)

转载 作者:行者123 更新时间:2023-11-30 09:46:37 29 4
gpt4 key购买 nike

我在使用带有 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com