如何在JavaScript中保存/重用对象?启动JavaScript对象并将其保存到变量
if (typeof window.Test == "undefined") window.Test = {};
if (typeof Test == "undefined") Test = {};
Test.Object1 = function() {
var obj = {
init: function(msg) {
console.log(msg);
},
EOF: null
};
return obj;
}();
Test.Test = function() {
var obj = {
init: function() {
var obj1 = Test.Object1.init('Object 1 initialized');
console.log(obj1);
},
EOF: null
};
return obj;
}();
Test.Test.init();
console.log(obj1)
回报undefined
var obj1 = new Test.Object1();
产生TypeError: Test.Object1 is not a constructor
你从未定义'测试'。你的代码中有'Test = {}'的地方吗? – romo 2013-05-07 16:29:21
是的,我做了,对不起忘了包括它 – keeg 2013-05-07 16:32:02