正在阅读:Converting mistakes into errors下的https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode,并想要做一个明确的例子。所以我去了JSFiddle,试着看看'use strict';到底实现了什么。代码如下:
(() => {
// Strict mode makes assignments which would otherwise silently fail to throw an exception.
'use strict';
try {
const undefined = 666; // throws a TypeError
} catch(error) {
console.error(error)
}
console.log('Is this read?');
})();https://jsfiddle.net/cvxau3m7/
我预计firebug中会出现一个错误。我一定是误解了这一点?
发布于 2017-10-26 17:05:27
创建一个名为undefined的常量没有错(除非已经在immediate作用域中创建了undefined变量)。
你的注释说“否则它会默默地失败”,但你的代码不会这样做。
(() => {
'use strict';
const undefined = "some value";
console.log("undefined is " + undefined);
})();
链接到全局范围内重新声明变量的示例。他们不会在更窄的范围内掩盖它们。
https://stackoverflow.com/questions/46949847
复制相似问题