// 定義一個動物類 function Animal (name) { // 屬性 this.name = name || ‘Animal’; // 實例方法 this.sleep = function(){ console.log(this.name + ‘正在睡覺!’); } } // 原型方法 Animal.prototype.eat = function(food) { console.log(this.name + ‘正在吃:’ + food); };
核心:將父類的實例做爲子類的原型, JavaScript常見的六種繼承方式
function Cat(){ } Cat.prototype = new Animal(); Cat.prototype.name = ‘cat’; // Test Code var cat = new Cat(); console.log(cat.name); console.log(cat.eat(‘fish’)); console.log(cat.sleep()); console.log(cat instanceof Animal); //true console.log(cat instanceof Cat); //true
特色:html
缺點:es6
下面代碼解釋缺點3(注意是引用屬性):segmentfault
function Super(){ this.val = 1; this.arr = [1]; } function Sub(){ // ... } Sub.prototype = new Super(); // 核心 var sub1 = new Sub(); var sub2 = new Sub(); sub1.val = 2; sub1.arr.push(2); alert(sub1.val); // 2 alert(sub2.val); // 1 alert(sub1.arr); // 1, 2 alert(sub2.arr); // 1, 2
核心:使用父類的構建函數來加強子類實例,等於複製父類的實例屬性給子類(沒用到原型),除了call方法,也能夠用apply()
function Cat(name){ Animal.call(this); this.name = name || ‘Tom’; } // Test Code var cat = new Cat(); console.log(cat.name); console.log(cat.sleep()); console.log(cat instanceof Animal); // false console.log(cat instanceof Cat); // true
特色:app
缺點:函數
核心:爲父類實例添加新特性,做爲子類實例返回
function Cat(name){ var instance = new Animal(); instance.name = name || ‘Tom’; return instance; } // Test Code var cat = new Cat(); console.log(cat.name); console.log(cat.sleep()); console.log(cat instanceof Animal); // true console.log(cat instanceof Cat); // false
特色:性能
缺點:this
核心:使用for...in將父類實例中的方法賦給子類實例
function Cat(name){ var animal = new Animal(); for(var p in animal){ Cat.prototype[p] = animal[p]; } Cat.prototype.name = name || ‘Tom’; } // Test Code var cat = new Cat(); console.log(cat.name); console.log(cat.sleep()); console.log(cat instanceof Animal); // false console.log(cat instanceof Cat); // true
特色:prototype
缺點:code
核心:經過調用父類構造,繼承父類的屬性並保留傳參的優勢,而後經過將父類實例做爲子類原型,實現函數複用
function Cat(name){ Animal.call(this); this.name = name || ‘Tom’; } Cat.prototype = new Animal(); //組合繼承須要修復構造函數的指向 Cat.prototype.constructor=Cat; // Test Code var cat = new Cat(); console.log(cat.name); console.log(cat.sleep()); console.log(cat instanceof Animal); // true console.log(cat instanceof Cat); // true
特色:htm
缺點:
核心:經過寄生方式,砍掉父類的實例屬性,這樣,在調用兩次父類的構造的時候,就不會初始化兩次實例方法/屬性,避免的組合繼承的缺點
function Cat(name){ Animal.call(this); this.name = name || ‘Tom’; } (function(){ // 建立一個沒有實例方法的類 var Super = function(){}; Super.prototype = Animal.prototype; //將實例做爲子類的原型 Cat.prototype = new Super(); //寄生組合繼承須要修復構造函數的指向 Cat.prototype.constructor=Cat; })(); // Test Code var cat = new Cat(); console.log(cat.name); console.log(cat.sleep()); console.log(cat instanceof Animal); // true console.log(cat instanceof Cat); //true
特色:
缺點:
補充:es6的class實現繼承
class Animal { //構造函數 constructor(props) { this.name = props.name || '未知'; } eat() { alert(this.name + "在吃東西..."); } } //class繼承 class Bird extends Animal { //構造函數 constructor(props) { //調用實現父類的構造函數 super(props); this.name = props.name || "未知"; } fly() { alert(this.name + "在飛..."); } } var myBird = new Bird({ name: '鸚鵡' }) myBird.eat() // 鸚鵡在吃東西... myBird.fly() // 鸚鵡在飛...