为什么我的JavaScript函数不能访问我的其他.js文件中定义的全局范围函数/变量?

时间:2022-08-24 16:23:58

I wrote a script like that:

我写了一个这样的剧本:

NS.load = function(src) {
    var script = document.createElement("script").setAttribute("src", src);
    document.getElementsByTagName("head")[0].appendChild(script);
}

It loads files but I can't reach functions and variables defiened in other files.

它会加载文件,但我无法访问其他文件中的函数和变量。

//js/main.js
var qux = {name: "name"};
NS.load("js/foo.js");

//js/foo.js
alert(qux.name); //undefined variable

But if I define qux like this:

但是如果我这样定义qux

window.qux = {name: "name"};

I can reach qux variable in other modules. As far as I know all globals are already a member of window object. So why I have to define variables like this. Could you offer another method?

我可以在其他模块中找到qux变量。据我所知,所有全局变量都已经是窗口对象的成员。为什么我要这样定义变量。你能再提供一种方法吗?

Thanks.

谢谢。

1 个解决方案

#1


7  

It looks like you tried to shortcut your code by calling createElement and setAttribute all on 1 line, but setAttribute doesn't return anything, so you can't go calling appendChild on it's return value, because there is none.This will fix it:

看起来您试图通过在一行中调用createElement和setAttribute来简化代码,但是setAttribute没有返回任何东西,所以您不能在返回值上调用appendChild,因为没有。这将改正:

NS.load = function(src) {
    var script = document.createElement("script");
    script.setAttribute("src", src)
    document.getElementsByTagName("head")[0].appendChild(script);
}

Edit:

编辑:

What sort of environment are you running your code in? Is something happening cross-site or are you defining qux inside of another function? The following works for me, running the files via http://localhost/test.html

你运行代码的环境是什么?是否发生了交叉站点或者在另一个函数中定义了qux ?下面的代码可以通过http://localhost/test.html运行文件

<html>
<head>
    <script type="text/javascript">
        load = function(src) {
            var script = document.createElement("script");
            script.setAttribute("src", src);
            document.getElementsByTagName("head")[0].appendChild(script);
        }
        var qux = {name: "name"};
        load("foo.js");
    </script>
</head>
<body></body>
</html>

foo.js:

foo.js:

alert(qux.name);

I get an alert with "name" when the page loads.

当页面加载时,我得到一个带有“name”的警告。

#1


7  

It looks like you tried to shortcut your code by calling createElement and setAttribute all on 1 line, but setAttribute doesn't return anything, so you can't go calling appendChild on it's return value, because there is none.This will fix it:

看起来您试图通过在一行中调用createElement和setAttribute来简化代码,但是setAttribute没有返回任何东西,所以您不能在返回值上调用appendChild,因为没有。这将改正:

NS.load = function(src) {
    var script = document.createElement("script");
    script.setAttribute("src", src)
    document.getElementsByTagName("head")[0].appendChild(script);
}

Edit:

编辑:

What sort of environment are you running your code in? Is something happening cross-site or are you defining qux inside of another function? The following works for me, running the files via http://localhost/test.html

你运行代码的环境是什么?是否发生了交叉站点或者在另一个函数中定义了qux ?下面的代码可以通过http://localhost/test.html运行文件

<html>
<head>
    <script type="text/javascript">
        load = function(src) {
            var script = document.createElement("script");
            script.setAttribute("src", src);
            document.getElementsByTagName("head")[0].appendChild(script);
        }
        var qux = {name: "name"};
        load("foo.js");
    </script>
</head>
<body></body>
</html>

foo.js:

foo.js:

alert(qux.name);

I get an alert with "name" when the page loads.

当页面加载时,我得到一个带有“name”的警告。