DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> JavaScript基礎知識 >> javascript學習筆記(十) js對象 繼承
javascript學習筆記(十) js對象 繼承
編輯:JavaScript基礎知識     
1.原型鏈
//很少單獨使用
復制代碼 代碼如下:
View Code
//定義 SuperClass類,有一個屬性property和一個方法getSuperValue
function SuperClass() {
this.property = true;
}
SuperClass.prototype.getSuperValue = function() {
return this.property;
}

//定義SubClass類,有一個屬性subproperty和後來添加的一個方法getSubValue
function SubClass() {
this.subproperty = false;
}

//SubClass類繼承SuperClass類
SubClass.prototype = new SuperClass();

//SubClass類添加一個方法getSubValue
SubClass.prototype.getSubValue = function() {
return this.subproperty;
}

//創建SubClass類的實例
var instance = new SubClass();
alert(instance.getSuperValue());

2. 確定原型與實例的關系
第一種方式用 instanceof 操作符,用來測試實例和原型鏈中出現過的構造函數
復制代碼 代碼如下:
alert(instance instanceof Object); //true ,instance是Object的實例嗎?
alert(instance instanceof SuperClass); //true ,instance是SuperClass的實例嗎?
alert(instance instanceof SubClass); //true,instance是SubClass的實例嗎?

第二種方式用 isPrototypeOf()方法,測試原型鏈中出現的原型
復制代碼 代碼如下:
alert(Object.prototype.isPrototypeOf(instance)); //true
alert(SuperClass.prototype.isPrototypeOf(instance)); //true
alert(SubClass.prototype.isPrototypeOf(instance)); //true

3. 用原型鏈繼承定義方法時的注意點
定義方法是的順序:
復制代碼 代碼如下:
View Code
function SuperClass() {
this.property = true;
}
SuperClass.prototype.getSuperValue = function() {
return this.property;
}

function SubClass() {
this.subproperty = false;
}

//SubClass繼承SuperClass
SubClass.prototype = new SuperClass(); //這個要先寫,新添加的方法和重寫超類的方法要寫在後面,否則重寫的超類方法將永遠無法調用

//添加新方法
SubClass.prototype.getSubValue = function() {
return this.subproperty;
}
//重寫超類的方法
SubClass.prototype.getSuperValue = function() {
return false;
}
var instance = new SubClass();
alert(instance.getSuperValue()); //fales,這裡SubClass的實例調用了SubClass的getSuperValue()方法,而屏蔽了SuperClass的getSuperValue()方法,
//使用SuperClass的方法會調用SuperClass的getSuperValue()方法

原型鏈繼承的缺點:1)共享超類中的屬性,2)在創建子類時不能向超類的構造函數傳遞參數。所有很少單獨使用原型鏈

4.借用構造函數
//很少單獨使用

優點:可以向超類傳遞參數 。缺點:函數無法復用,所有類都要使用構造函數模式
復制代碼 代碼如下:
View Code
function SuperClass(name) {
this.name = name;
}
function SubClass(){
SuperClass.call(this,"RuiLiang"); //繼承了SuperClass,同時向SuperClass傳遞了參數
this.age = 29; //實例屬性
}

var instance = new SubClass();
alert(instance.name); //RuiLiang
alert(instance.age); //29

6.組合繼承
//最常用的繼承模式
復制代碼 代碼如下:
View Code
//創建SuperClass
function SuperClass(name) {
this.name = name;
this.colors = ["red","blue","green"];
}
SuperClass.prototype.sayName = function() {
alert(this.name);
}

////創建SubClass
function SubClass(name,age) {
SuperClass.call(this,name); //繼承屬性
this.age = age; //自己的屬性
}

SubClass.prototype = new SuperClass(); //繼承方法
SubClass.prototype.sayAge = function() { //SubClass添加新方法
alert(this.age);
};

//使用
var instance1 = new SubClass("RuiLiang",30);
instance1.colors.push("black");
alert(instance1.colors); //"red,blue,green,black"
instance1.sayName(); //"RuiLiang"
instance1.sayAge(); //30

var instance2 = new SubClass("XuZuNan",26);
alert(instance2.colors); //"red,blue,green"
instance2.sayName(); //"RuiLiang"
instance2.sayAge(); //30

7.其它繼承模式
原型式繼承、寄生式繼承、寄生組合式繼承
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved