js繼承的幾種方法

2021-09-11 19:04:51 字數 1655 閱讀 4585

//繼承的幾種方法

//1.傳統形式 繼承過多沒用的東西

// father.prototype.name = 'hzl';

// function father()

// var father = new father();

// son.prototype = father;

// function son()

// var son = new son();

// console.log(son.***); //male

// console.log(son.name); // hzl

// 2.借用建構函式 不能整合建構函式原型 每次構造都要多走乙個函式

// function father(name,***,age)

// function son(name, ***, age, marry)

// var son = new son('1','2','3','4');

//3.共享模式

// father prototype

// father son

// 缺點 一旦改son的原型father原型也改動

// 此時father和son的引用指向同乙個房間 堆資料

// father.prototype.name = 'hzl';

// function father()

// son.prototype = father.prototype;

// function son()

// var son = new son();

//4. 聖杯模式

// father.prototype.name = 'hzl'

// function father()

// function son()

// function inherit(target,origin);

// nul.prototype = origin.prototype;

// target.prototype = nul.prototype;

// target.prototype.constructor = target; 沒有這一句constructor返回father(){}

// target.prototype.uber = origin.prototype; 記錄最終繼承自誰的原型

// }

// inherit(son,father);

// var son = new son;

//改善 yui3 建議

// father.prototype.name = 'hzl'

// function father()

// function son()

// var inherit = (function();

// return function(target,origin)

// }())

// inherit(son,father);

// var son = new son;

JS實現繼承的幾種方法

call方法的第乙個引數的值賦值給類 即方法 中出現的 this call方法的第二個引數開始依次賦值給類 即方法 所接受的引數 call 相同,第二個引數為陣列型別,這個陣列中的每個元素依次賦值給類 即方法 所接受的引數 this 的指向,我們就是利用它的這個特性來實現繼承的。補充知識點 func...

js實現繼承的幾種方法

js實習繼承的幾種方法 1.for in繼承 function person function son var p new person var s new son for var k in p console.log s.name 水煮魚 console.log s.age 18 2.原型繼承 f...

js原型繼承的幾種方法

首先建立乙個建構函式,並為其設定私有屬性和公有屬性。定義乙個人類 function person name 原型方法 person.prototype.eat function food 原型繼承有很多種方法,接下來就記錄下我已知的幾種方法。第一種 原型鏈繼承 重點圈起來 將父類例項賦值給子類原型物...