JavaScript是如何實(shí)現(xiàn)繼承的(六種方式)
前言:大多OO語(yǔ)言都支持兩種繼承方式: 接口繼承和實(shí)現(xiàn)繼承 ,而ECMAScript中無(wú)法實(shí)現(xiàn)接口繼承,ECMAScript只支持實(shí)現(xiàn)繼承,而且其實(shí)現(xiàn)繼承主要是依靠 原型鏈 來(lái)實(shí)現(xiàn)。
1.原型鏈
基本思想:利用原型讓一個(gè)引用類型繼承另外一個(gè)引用類型的屬性和方法。
構(gòu)造函數(shù),原型,實(shí)例之間的關(guān)系:每個(gè)構(gòu)造函數(shù)都有一個(gè)原型對(duì)象,原型對(duì)象包含一個(gè)指向構(gòu)造函數(shù)的指針,而實(shí)例都包含一個(gè)指向原型對(duì)象的內(nèi)部指針。
原型鏈實(shí)現(xiàn)繼承例子:
function SuperType() {
this.property = true;
}
SuperType.prototype.getSuperValue = function() {
return this.property;
}
function subType() {
this.property = false;
}
//繼承了SuperType
SubType.prototype = new SuperType();
SubType.prototype.getSubValue = function (){
return this.property;
}
var instance = new SubType();
console.log(instance.getSuperValue());//true
2.借用構(gòu)造函數(shù)
基本思想:在子類型構(gòu)造函數(shù)的內(nèi)部調(diào)用超類構(gòu)造函數(shù),通過(guò)使用call()和apply()方法可以在新創(chuàng)建的對(duì)象上執(zhí)行構(gòu)造函數(shù)。
例子:
function SuperType() {
this.colors = ["red","blue","green"];
}
function SubType() {
SuperType.call(this);//繼承了SuperType
}
var instance1 = new SubType();
instance1.colors.push("black");
console.log(instance1.colors);//"red","blue","green","black"
var instance2 = new SubType();
console.log(instance2.colors);//"red","blue","green"
3.組合繼承
基本思想:將原型鏈和借用構(gòu)造函數(shù)的技術(shù)組合在一塊,從而發(fā)揮兩者之長(zhǎng)的一種繼承模式。
例子:
function SuperType(name) {
this.name = name;
this.colors = ["red","blue","green"];
}
SuperType.prototype.sayName = function() {
console.log(this.name);
}
function SubType(name, age) {
SuperType.call(this,name);//繼承屬性
this.age = age;
}
//繼承方法
SubType.prototype = new SuperType();
Subtype.prototype.constructor = Subtype;
Subtype.prototype.sayAge = function() {
console.log(this.age);
}
var instance1 = new SubType("EvanChen",18);
instance1.colors.push("black");
consol.log(instance1.colors);//"red","blue","green","black"
instance1.sayName();//"EvanChen"
instance1.sayAge();//18
var instance2 = new SubType("EvanChen666",20);
console.log(instance2.colors);//"red","blue","green"
instance2.sayName();//"EvanChen666"
instance2.sayAge();//20
4.原型式繼承
基本想法:借助原型可以基于已有的對(duì)象創(chuàng)建新對(duì)象,同時(shí)還不必須因此創(chuàng)建自定義的類型。
原型式繼承的思想可用以下函數(shù)來(lái)說(shuō)明:
function object(o) {
function F(){}
F.prototype = o;
return new F();
}
例子:
var person = {
name:"EvanChen",
friends:["Shelby","Court","Van"];
};
var anotherPerson = object(person);
anotherPerson.name = "Greg";
anotherPerson.friends.push("Rob");
var yetAnotherPerson = object(person);
yetAnotherPerson.name = "Linda";
yetAnotherPerson.friends.push("Barbie");
console.log(person.friends);//"Shelby","Court","Van","Rob","Barbie"
ECMAScript5通過(guò)新增Object.create()方法規(guī)范化了原型式繼承,這個(gè)方法接收兩個(gè)參數(shù):一個(gè)用作新對(duì)象原型的對(duì)象和一個(gè)作為新對(duì)象定義額外屬性的對(duì)象。
var person = {
name:"EvanChen",
friends:["Shelby","Court","Van"];
};
var anotherPerson = Object.create(person);
anotherPerson.name = "Greg";
anotherPerson.friends.push("Rob");
var yetAnotherPerson = Object.create(person);
yetAnotherPerson.name = "Linda";
yetAnotherPerson.friends.push("Barbie");
console.log(person.friends);//"Shelby","Court","Van","Rob","Barbie"
5.寄生式繼承
基本思想:創(chuàng)建一個(gè)僅用于封裝繼承過(guò)程的函數(shù),該函數(shù)在內(nèi)部以某種方式來(lái)增強(qiáng)對(duì)象,最后再像真正是它做了所有工作一樣返回對(duì)象。
例子:
function createAnother(original) {
var clone = object(original);
clone.sayHi = function () {
alert("hi");
};
return clone;
}
var person = {
name:"EvanChen",
friends:["Shelby","Court","Van"];
};
var anotherPerson = createAnother(person);
anotherPerson.sayHi();///"hi"
6.寄生組合式繼承
基本思想:通過(guò)借用函數(shù)來(lái)繼承屬性,通過(guò)原型鏈的混成形式來(lái)繼承方法
其基本模型如下所示:
function inheritProperty(subType, superType) {
var prototype = object(superType.prototype);//創(chuàng)建對(duì)象
prototype.constructor = subType;//增強(qiáng)對(duì)象
subType.prototype = prototype;//指定對(duì)象
}
例子:
function SuperType(name){
this.name = name;
this.colors = ["red","blue","green"];
}
SuperType.prototype.sayName = function (){
alert(this.name);
};
function SubType(name,age){
SuperType.call(this,name);
this.age = age;
}
inheritProperty(SubType,SuperType);
SubType.prototype.sayAge = function() {
alert(this.age);
}
以上內(nèi)容給大家介紹了javascript實(shí)現(xiàn)繼承的六種方式,希望對(duì)大家有所幫助!
相關(guān)文章
簡(jiǎn)單了解微信小程序的目錄結(jié)構(gòu)
這篇文章主要介紹了簡(jiǎn)單了解小程序的目錄結(jié)構(gòu),在開(kāi)發(fā)小程序之前,我們首先需要對(duì)其目錄結(jié)構(gòu)進(jìn)行了解,以便于提升開(kāi)發(fā)效率,需要的朋友可以參考下2019-07-07
多瀏覽器兼容的動(dòng)態(tài)加載 JavaScript 與 CSS
Omar AL Zabir這位MVP總是喜歡搞些稀奇古怪同時(shí)又很實(shí)用的小東西,并且還十分值得參考。最近他就做了一個(gè)叫做ensure的小工具用于動(dòng)態(tài)加載JavaScript、CSS與HTML,而且IE、Firefox、Opera、Safari都支持了,那么我們就來(lái)看看ensure是如何做到動(dòng)態(tài)加載JavaScript與CSS的。2008-09-09
JavaScript iframe 實(shí)現(xiàn)多窗口通信實(shí)例詳解
這篇文章主要為大家介紹了JavaScript iframe 實(shí)現(xiàn)多窗口通信實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
JS中的算法與數(shù)據(jù)結(jié)構(gòu)之集合(Set)實(shí)例詳解
這篇文章主要介紹了JS中的算法與數(shù)據(jù)結(jié)構(gòu)之集合(Set),結(jié)合實(shí)例形式詳細(xì)分析了javascript中集合的概念、原理、定義及相關(guān)操作技巧,需要的朋友可以參考下2019-08-08
JavaScript避免代碼的重復(fù)執(zhí)行經(jīng)驗(yàn)技巧分享
經(jīng)常會(huì)發(fā)現(xiàn)一個(gè)問(wèn)題,那就是重復(fù)的代碼執(zhí)行,下面就是一些在查看它們的源代碼時(shí)發(fā)現(xiàn)一些問(wèn)題,把這些分享給大家,希望能讓你們更加簡(jiǎn)潔高效的寫出JavaScript代碼2014-04-04
js獲取GridView中行數(shù)據(jù)的兩種方法 分享
這篇文章介紹了js獲取GridView中行數(shù)據(jù)的方法,有需要的朋友可以參考一下2013-07-07
javascript下有關(guān)dom以及xml節(jié)點(diǎn)訪問(wèn)兼容問(wèn)題
javascript下有關(guān)dom以及xml節(jié)點(diǎn)訪問(wèn)兼容問(wèn)題...2007-11-11
純js代碼制作的網(wǎng)頁(yè)時(shí)鐘特效【附實(shí)例】
下面小編就為大家?guī)?lái)一篇純js代碼制作的網(wǎng)頁(yè)時(shí)鐘特效【附實(shí)例】。小編覺(jué)得聽(tīng)錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-03-03

