简体   繁体   English

为什么此代码无法运行?

[英]why this code doesn't run?

(function () {
    "use strict";


    function initialize() {
        myList = ['one', 'two', 'three'];
    }

    function displayList() {
        var i, n;
        for (i = 0, n = myList.length; i < n; i += 1) {
            alert(myList[i]);
        }
    }
    initialize();
    displayList();

})();

if not using var, the myList variable will supposedly be created as a globle variable. 如果不使用var,则myList变量将被创建为全局变量。 Either way, the code should be running. 无论哪种方式,代码都应该正在运行。 What is wrong with the code?? 代码有什么问题??

myList = ['one', 'two', 'three'];

In strict mode, you are not allowed to create global variables in this way. 在严格模式下,不允许您以这种方式创建全局变量。

From official Mozilla documentation - 来自Mozilla官方文档 -

First, strict mode makes it impossible to accidentally create global variables. 首先,严格模式使得不可能意外地创建全局变量。 In normal JavaScript mistyping a variable in an assignment creates a new property on the global object and continues to "work" (although future failure is possible: likely, in modern JavaScript). 在正常的JavaScript错误输入中,赋值中的变量会在全局对象上创建一个新属性并继续“工作”(尽管未来可能会失败:可能在现代JavaScript中)。 Assignments which would accidentally create global variables instead throw in strict mode: 会意外创建全局变量的赋值会改为以严格模式抛出:

"use strict"; “严格使用”;

mistypedVaraible = 17; mistypedVaraible = 17; // throws a ReferenceError //引发ReferenceError

This works - 这工作 -

(function () {
    "use strict";

    var myList;

    function initialize() {
        myList = ['one', 'two', 'three'];
    }

    function displayList() {
        var i, n;
        for (i = 0, n = myList.length; i < n; i += 1) {
            alert(myList[i]);
        }
    }

    initialize();
    displayList();
})();

You cannot set global variables like that when in strict mode. 在严格模式下,您无法设置类似的全局变量。

You'll have to do 你必须这样做

(function () {
    "use strict";

    var myList;

    function initialize() {
        myList = ['one', 'two', 'three'];
    }

    function displayList() {
        var i, n;
        for (i = 0, n = myList.length; i < n; i += 1) {
            alert(myList[i]);
        }
    }
    initialize();
    displayList();

})();

By using "use strict" you're limiting yourself to strict mode (which is a good thing), but that means you can't just use a variable that hasn't been set yet. 通过使用"use strict"您将自己限制为严格模式(这是一件好事),但这意味着您不能只使用尚未设置的变量。

If you want to define myList as a global variable, you'll have to do that before the function starts, so at the top of the script put: var myList; 如果要将myList定义为全局变量,则必须在函数启动之前执行此操作,因此在脚本顶部放置: var myList;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么这段代码的其他部分不运行? - Why doesn't the else part of this code run? 为什么MutationObserver代码不能在Chrome 30上运行? - Why doesn't MutationObserver code run on Chrome 30? 为什么这个承诺调用之后的代码不运行? - Why doesn't the code after this promise invocation run? 为什么我的代码无法运行? 插入排序 - Why my code doesn't run ? Insertion Sort 为什么这段 JS 代码不能在旧设备上运行? - Why doesn't this JS code run on older devices? 这个简单的代码无法运行 - This simple code doesn't run 为什么 await 之后的代码没有立即运行? 不应该是非阻塞的吗? - Why doesn't the code after await run right away? Isn't it supposed to be non-blocking? 为什么此代码不运行,而是向我发送此错误:(未处理的拒绝(类型错误):this.state 不是函数) - Why doesn't this code run, and instead sends me this error:(Unhandled Rejection (TypeError): this.state is not a function) 为什么这个 JavaScript ES6 示例中“等待”之后的代码没有运行? - Why doesn't the code after “await” in this JavaScript ES6 example get run? 为什么此xhtml文件显示JS代码但不运行该功能 - Why does this xhtml file display JS code but doesn't run the function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM