首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JSFiddle中“use strict”示例

JSFiddle中“use strict”示例
EN

Stack Overflow用户
提问于 2017-10-26 16:54:27
回答 1查看 209关注 0票数 2

正在阅读:Converting mistakes into errors下的https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode,并想要做一个明确的例子。所以我去了JSFiddle,试着看看'use strict';到底实现了什么。代码如下:

代码语言:javascript
复制
(() => {
    // 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中会出现一个错误。我一定是误解了这一点?

EN

回答 1

Stack Overflow用户

发布于 2017-10-26 17:05:27

创建一个名为undefined的常量没有错(除非已经在immediate作用域中创建了undefined变量)。

你的注释说“否则它会默默地失败”,但你的代码不会这样做。

代码语言:javascript
复制
(() => {
    'use strict';
    const undefined = "some value"; 
    console.log("undefined is " + undefined);
})();

链接到全局范围内重新声明变量的示例。他们不会在更窄的范围内掩盖它们。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46949847

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档