简体   繁体   English

Javascript ES6在全局范围内而不是在窗口范围内引用变量

[英]Javascript ES6 reference a variable in the global scope but not in the window scope

I am upgrading my code from ES5 to ES6. 我正在将代码从ES5升级到ES6。 In ES5 I dynamically load the function by creating a new script which then loads the file asynchronously. 在ES5中,我通过创建一个新脚本来动态加载该函数,该脚本随后异步加载该文件。 the code is as follows: 代码如下:

loadClass : function(className) {
    if(typeof window[className] !== "function") {
        page.getCode(className, dirName);
        return;
    } ...


getCode(className) {
    var fileName = className.toLowerCase();
    var newScript = document.createElement('script');
    newScript.setAttribute("type", "text/javascript");
    newScript.setAttribute("src", '/public/js/' + fileName + '.js');
    var parmArr = [className];
    newScript.addEventListener('load', this.loadClass.bind("", parmArr));
    document.getElementsByTagName("head")[0].appendChild(newScript);
    return;
}

In ES5 the function is loaded in the global scope which in this case is window. 在ES5中,该函数加载在全局范围内,在这种情况下为window。 I have the function/class name in a string and I can reference the class using window[classname] as shown in the code above. 我在字符串中有函数/类名,可以使用window [classname]引用该类,如上面的代码所示。 In ES6 I changed the calling and called functions to a class. 在ES6中,我将调用和被调用函数更改为一个类。 Now when it loads the script it exists as Myclass function() in the global scope but not in the window scope as shown in the Chrome debugger like so: 现在,当它加载脚本时,它作为Myclass function()存在于全局作用域中,但不存在于窗口作用域中,如Chrome调试器中所示,如下所示:

Myclass ()
[[Scopes]] : Scopes[2]
    0 : Script
        Myclass () : () 

I can no longer reference it with a variable as window[classname]. 我无法再使用window [classname]作为变量来引用它。 Grasping at straws I also tried [classname], global[classname] and Global[classname] and this[classname] and script[classname] but that does not work. 我也尝试过使用[classname],global [classname]和Global [classname]以及this [classname]和script [classname],但这是行不通的。

In ES6, how do I test if it is already loaded when the classname is in a variable ? 在ES6中,当类名位于变量中时,如何测试它是否已加载? This is probably something really easy, but it is eluding me. 这可能确实很容易,但却使我难以理解。

Thanks in advance 提前致谢

This is how ES6 works... To avoid polluting the global object (here window ), your JavaScript code does not create global properties anymore (unless you are explicit). ES6的工作方式是...为了避免污染全局对象(此处为window ),您的JavaScript代码不再创建全局属性(除非您是明确的)。 In ES5, these properties were created automatically when using var or function in the global scope. 在ES5中,在全局范围内使用varfunction时会自动创建这些属性。 With let , const or class , nothing happens. 使用letconstclass ,什么都不会发生。 Consider this example: 考虑以下示例:

// ES5

var foo;

function qux() {}

console.log('foo' in window); // true
console.log('qux' in window); // true

// ES6

let bar;
const baz = '';

class Quux {}

console.log('bar' in window); // false
console.log('baz' in window); // false
console.log('Quux' in window); // false

window.bar = bar;
window.baz = baz;
window.Quux = Quux;

console.log('bar' in window); // true
console.log('baz' in window); // true
console.log('Quux' in window); // true

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM