我已經搜索並閱讀了幾個小時,但仍無法理解用於創建具有不同方法選擇的新對象的基本設計模式(具有相同名稱),其取決於其中一個參數。這裏有一些代碼來解釋我正在嘗試做什麼。 歡迎提供所有建議和替代方法。我希望有人能解放我,形成這種無知的雲。 感謝根據對象創建期間的參數選擇對象所具有的方法集合 - 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