如何在 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