這是很簡單的事,它繼承應用程序是偉大的:
您只需返回任何你想要的,也許通過方法返回的,後來在其他功能上重新使用它,並建立它。
舉例如下:
function a(){
var b;
//assignment to b and other stuff
return b;
};
// or
function a(){
var b, result;
//assignment to b and other stuff
returnInitial: function() {
return b;
}
// other stuff with b
return result;
};
稍後,可以使用所謂的「寄生繼承」,用所有局部變量和添加新的方法啓動等功能,這裏面全功能的,就像這樣:
var a function() {
var b, result;
//assignment to b and other stuff
returnInitial: function() {
return b;
}
// other stuff with b
return result;
}
var extendedA function() {
var base = new a;
var b = a.returnInitial();
a.addToB = function (c) {
var sum = c + a.returnInitial();
return sum;
}
}
所以,你現在可以得到
var smt = new extendA();
var c = 12; //some number
var sumBC = extendA.addToB(c);
對於所有有這些偉大PRACT我建議yutube搜索道格克羅克福德關於js對象處理的講座。
請注意,如果您未初始化新實例,則需要使用新的動態對象處理方法,因爲JavaScript使用的動態對象處理可能會使原始對象崩潰。
你不能直接這樣做,將'b'分配爲'this'的屬性,它將是'c'的屬性。要麼是,要麼附加一個返回'b'的值的方法/函數。 (你將不得不在構造函數中創建該函數,它也不能用於原型方法) – 2013-03-02 20:21:31
我認爲閉包在這種情況下會有幫助嗎?但我可能是錯的。 – aug 2013-03-02 20:27:07
代碼不是我的,但我需要僅使用腳本和Scriptish來更改它。我無法做到全球化,故意將它製作成本地的,以防止用戶寫作。 還有一次:我在寫USERSCRIPT。使用FIDDLER及其腳本引擎也不是很好的解決方案。 – KOLANICH 2013-03-02 20:47:02