[英]Example of 'use strict' in JSFiddle
正在閱讀: https : //developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode下的Converting mistakes into errors
,想舉個明確的例子。 所以我轉到 JSFiddle 並嘗試查看'use strict';
是什么'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/
我期待在螢火蟲中出現錯誤。 我必須以某種方式誤解了這一點?
創建一個名為undefined
的常量並沒有錯(除非在直接作用域中已經創建了一個undefined
變量)。
您的評論說“否則會默默地失敗”,但您的代碼不會這樣做。
(() => { 'use strict'; const undefined = "some value"; console.log("undefined is " + undefined); })();
您鏈接到重新聲明全局范圍內的變量的示例。 他們並沒有在更窄的范圍內掩蓋它們。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.