“use strict”; + jQuery.getScript() = script can't export to global namespace

断了今生、忘了曾经 提交于 2019-11-30 15:15:35

It uses $.getScript() which uses eval to execute the script which cannot modify the global scope in strict mode:

Second, eval of strict mode code does not introduce new variables into the surrounding scope. In normal code eval("var x;") introduces a variable x into the surrounding function or the global scope. This means that, in general, in a function containing a call to eval every name not referring to an argument or local variable must be mapped to a particular definition at runtime (because that eval might have introduced a new variable that would hide the outer variable). In strict mode eval creates variables only for the code being evaluated, so eval can't affect whether a name refers to an outer variable or some local variable:

Source: https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Functions_and_function_scope/Strict_mode

The solution is not using jQuery to load the script but appending a script element to the DOM. Note that you cannot even use jQuery to append the element; it will automatically use $.getScript() for it.

jQuery evaluates the script. "use strict;" inside eval changes the semantics of the code. That's why strict mode can be dangerous! Because in browsers which don't support it, your code does something else.

Second, eval of strict mode code does not introduce new variables into the surrounding scope. In normal code eval("var x;") introduces a variable x into the surrounding function or the global scope. This means that, in general, in a function containing a call to eval every name not referring to an argument or local variable must be mapped to a particular definition at runtime (because that eval might have introduced a new variable that would hide the outer variable). In strict mode eval creates variables only for the code being evaluated, so eval can't affect whether a name refers to an outer variable or some local variable.

https://developer.mozilla.org/en/JavaScript/Strict_mode

The solution is to use window.foo = "bar;" instead of var foo = "bar";. And you should put your code inside a IIFE to make "use strict;" more predictable.

(function(window) {
    "use strict";

    window.globalVar = {};
    alert(typeof window.globalVar);
}(window));

Process the response removing the strict mode before execute:

 $.ajax({
    url: scriptUrl,
    dataType: "script",
    dataFilter: function (data, type) {
        return data.replace("\'use strict\';", "");
    }
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!