我不知道我是否正確地描述這個,但我試圖使用靜態成員來處理存儲和提取對象實例,但它是不工作,並且不會在FireBug中引發錯誤。當它到達MyClass.instances [id] = new MyClass(cfg);執行就停止了。試圖註冊使用靜態方法和靜態哈希對象的對象的多個實例
/*
I want to be able to call like so:
MyClass.register('34', cfg);
and then use like...
MyClass.get('34').someMeth();
*/
/* MyClass */
var MyClass = function(config){
this.init (config);
};
/* static var to hold instances */
MyClass.instances = {};
/* static method to register an instance */
MyClass.register = function (id, $cfg) {
//this is where it goes poof.... no error just stops
MyClass.instances[id] = new MyClass(cfg);
return;
}
/* static method to get an instance */
MyClass.get = function (id) {
return MyClass.instances[id];
}
/* object instance methods here */
MyClass.prototype = {
init: function() {
},
someMeth: function() {
},
}
對於JS的工廠模式的啓動認爲這會發生什麼:http://2007-2010.lovemikeg.com/2009/06/26/the-工廠模式在JavaScript/ – Drew 2011-05-20 03:47:06