我已经搜索并阅读了几个小时,但仍无法理解用于创建具有不同方法选择的新对象的基本设计模式(具有相同名称),其取决于其中一个参数。这里有一些代码来解释我正在尝试做什么。 欢迎提供所有建议和替代方法。我希望有人能解放我,形成这种无知的云。 感谢根据对象创建期间的参数选择对象所具有的方法集合 - JavaScript
function BaseConstructor(whichMethods) {
if (whichMethods==='a') {
// do something to incorporate methodSetA
}
else if (whichMethods==='b') {
// do something to incorporate methodSetB
}
this.init();
};
var methodSetA = {
init: function() {
// do initialisation A way
},
speak: function() {
alert('i speak AAA way')
}
};
var methodSetB = {
init: function() {
// do initialisation B way
},
speak: function(){
alert('i got BBB all the way')
}
};
thing = new BaseConstructor('b');
// b is an instance of BaseConstructor and has done the bWay init() function
thing.speak() // return alert 'i got BBB all the way'
明白了,感谢您的及时回复!工厂设计模式 - 我今天肯定已经阅读了5次,并且有点认为这是我需要的,但总是在无关的细节中分散注意力。再次感谢@ jfriend00 – johowie 2012-01-05 03:02:50