JS中的繼承,重點 聖杯模式

2021-08-31 16:25:06 字數 1080 閱讀 5036

1.原型鏈的繼承

grand.prototype.lastname = 『ji』;

function grand()

var grand = new grand();

father.prototype = grand;

function father()

var father = new father();

son.prototype = father;

function son()

var son = new son();

缺點:不僅繼承了grand的lastname,而且繼承了father的name;

2.借用建構函式:

不能繼承借用建構函式的原型,每次建構函式都要多走乙個函式

3.共享原型

father.prototype.lastname = 『deng』;

function father()

function son()

function inherit(target ,origin)

inherit(son,father);

//extend inherit繼承font-size : inherit(自動繼承父親的font-size(預設值))

不能隨便改動自己的原型(改變乙個父子都會改變)

4.聖杯模式

function inherit(target, origin);

f.prototype = origin.prototype;

target.prototype = new f();

target.prototype.constuctor = target;

target.prototype.uber = origin.prototype;

}//son.__proto__ -->new f().__proto__ -->father.prototype

var inherit = (function();    //閉包,變成私有化變數,在函式外部無法呼叫

return function(target, origin)

}());

js原型繼承,聖杯模式繼承

1.call 可以呼叫函式 2.call 可以修改this指向,使用call 的時候,第乙個引數是修改後的this指向,引數2,引數3 使用逗號隔開 function fun x,y var obj call 可以呼叫函式 fun.call call 可以改變這個函式的this指向 此時這個函式的t...

JS 聖杯模式(原型鏈繼承)

聖杯模式 為了son繼承father原型上的東西,還可以修改自己原型上的東西,對father原型不影響。function inherit target,origin 函式f作為乙個中間層,上連father,下連son,使兩函式互不干擾 f.prototype origin.prototype tar...

Java中的繼承 過載 覆蓋

第一節 繼承的概念 1.1.1 為什麼使用繼承 使乙個類的資料成員和成員方法可以被另乙個類 派生類或子類 重用。繼承的特性實現了真正意義上的 復用,子類可以從父類繼承所有非private的資料成員和成員方法,以體現其共性。在子類中也可以新定義一些自己特有的資料成員和成員方法,以體現其個性。1.1.2...