JavaScript是如何實現(xiàn)繼承的(六種方式)
前言:大多OO語言都支持兩種繼承方式: 接口繼承和實現(xiàn)繼承 ,而ECMAScript中無法實現(xiàn)接口繼承,ECMAScript只支持實現(xiàn)繼承,而且其實現(xiàn)繼承主要是依靠 原型鏈 來實現(xiàn)。
1.原型鏈
基本思想:利用原型讓一個引用類型繼承另外一個引用類型的屬性和方法。
構(gòu)造函數(shù),原型,實例之間的關(guān)系:每個構(gòu)造函數(shù)都有一個原型對象,原型對象包含一個指向構(gòu)造函數(shù)的指針,而實例都包含一個指向原型對象的內(nèi)部指針。
原型鏈實現(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ù),通過使用call()和apply()方法可以在新創(chuàng)建的對象上執(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ā)揮兩者之長的一種繼承模式。
例子:
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.原型式繼承
基本想法:借助原型可以基于已有的對象創(chuàng)建新對象,同時還不必須因此創(chuàng)建自定義的類型。
原型式繼承的思想可用以下函數(shù)來說明:
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通過新增Object.create()方法規(guī)范化了原型式繼承,這個方法接收兩個參數(shù):一個用作新對象原型的對象和一個作為新對象定義額外屬性的對象。
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)建一個僅用于封裝繼承過程的函數(shù),該函數(shù)在內(nèi)部以某種方式來增強對象,最后再像真正是它做了所有工作一樣返回對象。
例子:
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.寄生組合式繼承
基本思想:通過借用函數(shù)來繼承屬性,通過原型鏈的混成形式來繼承方法
其基本模型如下所示:
function inheritProperty(subType, superType) { var prototype = object(superType.prototype);//創(chuàng)建對象 prototype.constructor = subType;//增強對象 subType.prototype = prototype;//指定對象 }
例子:
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實現(xiàn)繼承的六種方式,希望對大家有所幫助!
相關(guān)文章
一文讓你徹底弄懂js中undefined和null的區(qū)別
JavaScript是一門動態(tài)類型語言,元素除了表示存在的空值外,還有可能根本就不存在,這就是undefined存在的原因,這篇文章主要給大家介紹了關(guān)于undefined和null區(qū)別的相關(guān)資料,需要的朋友可以參考下2022-03-03js數(shù)組循環(huán)遍歷數(shù)組內(nèi)所有元素的方法
在js中數(shù)組遍歷最簡單的辦法就是使用for然后再利用arr.length長度作為for最大限度值即可解決了,下面我們來看看一些有用的實例2014-01-01JavaScript屏蔽Backspace鍵的實現(xiàn)代碼
這篇文章主要介紹了JavaScript屏蔽Backspace鍵的實現(xiàn)代碼,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-11-11微信小程序注冊60s倒計時功能 使用JS實現(xiàn)注冊60s倒計時功能
這篇文章主要介紹了微信小程序注冊60s倒計時功能,以及使用JS實現(xiàn)注冊60s倒計時功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08