【发布时间】:2012-01-05 02:46:45
【问题描述】:
我已经搜索和阅读了几个小时,但我仍然无法理解创建一个新对象的基本设计模式,该对象可以选择取决于其中一个参数设置的不同方法(同名)。这是一些代码来解释我正在尝试做的事情。 欢迎所有建议和替代方法。我希望有人能把我从这片无知的阴云中解放出来。 谢谢
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'
【问题讨论】:
标签: javascript interface new-operator