秒console.log()
執行WSFunctions[this.name]();
將打印undentified
。我想知道我是否能夠inherit
DoThisAndThat
在我的Call()
函數。我不想以WSFunctions[this.name](this.params)
的方式傳遞參數,因爲項目增長時可能會有超過this.params
的通過。將參數傳遞給對象或繼承對象的函數?
function WS(name, params) {
this.name = name;
this.params = params;
}
WS.prototype.Call = function() {
if (typeof WSFunctions[this.name] !== "function") {
return false;
}
console.log(this.params);
WSFunctions[this.name]();
return true;
}
var WSFunctions = {
'ScreenRightGuest': function() {
// .. whatever ..
return true;
},
'DoThisAndThat': function() {
console.log(this.params);
return true;
}
}
new WS('DoThisAndThat', { login: '123', pass: 'abc' }).Call();
在此先感謝 麥克
或者你可能有不同的建議,如何構建,我剛剛開始使用JavaScript「類」。 – Mike 2012-08-14 14:51:09
這與「類」或繼承或原型沒有任何關係,只與「this」和調用函數的工作方式有關, – 2012-08-14 14:56:31
是的這是一個非常奇怪的模式。您正在構建對象的實例,並使用此對象的「屬性」引用該類型的「靜態函數」。 – dievardump 2012-08-14 14:56:33