1.使用call或apply綁定構造函數
animal.apply(this.arguments)
2.使用prototype屬性
Cat.prototype = new Animal(); Cat.prototype.constructor = Cat; var cat1 = new Cat("大毛","黃色"); alert(cat1.species); // 動物3.直接集成prototype屬性
function Animal(){ } Animal.prototype.species = "動物"; Cat.prototype = Animal.prototype; Cat.prototype.constructor = Cat; var cat1 = new Cat("大毛","黃色"); alert(cat1.species); // 動物4.利用空對象作為中介
var F = function(){}; F.prototype = Animal.prototype; Cat.prototype = new F(); Cat.prototype.constructor = Cat; 將上面的方法封裝成一個函數,便于使用: function extend(Child, Parent) { var F = function(){}; F.prototype = Parent.prototype; Child.prototype = new F(); Child.prototype.constructor = Child; Child.uber = Parent.prototype; }5.拷貝繼承
function extend2(Child, Parent) { var p = Parent.prototype; var c = Child.prototype; for (var i in p) { c[i] = p[i]; } c.uber = p; }這個函數的作用,就是將父對象的prototype對象中的屬性,一一拷貝給Child對象的prototype對象。
以上這篇基于構造函數的五種繼承方法小結就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。
新聞熱點
疑難解答