我有这个myObject在foo.js中进行了decalred,并在bar.js中进行了修改。带getScript的jQuery全局变量
我试图在getScript完成时得到结果。然而,如果在jQuery中对其进行decalred,似乎myObject不是全局变量。
有人可以向我解释这个吗?
foo.js
/*
// this works fine, but I want to decalre it inside jQuery
var myObject = {
str: "hello world",
num: "123"
};
*/
$(function() {
// this makes myObject NOT a global valriable
var myObject = {
str: "hello world",
num: "123"
};
alert(myObject.str + " " + myObject.num);
$.getScript("bar.js", function() {
alert(myObject.str + " " + myObject.num);
});
});
bar.js
$(function() {
myObject = {
str: "new string",
num: "999"
};
});
是一样宣布它外面 – 2014-10-10 06:17:08
@ArunPJohny是它们具有相同的范围,而我在我的问题说......我希望变量是**在** jQuery中声明。 – user1643156 2014-10-10 06:30:06