JS實現(xiàn)面向?qū)ο罄^承的5種方式分析
本文實例講述了JS實現(xiàn)面向?qū)ο罄^承的5種方式。分享給大家供大家參考,具體如下:
js是門靈活的語言,實現(xiàn)一種功能往往有多種做法,ECMAScript沒有明確的繼承機制,而是通過模仿實現(xiàn)的,根據(jù)js語言的本身的特性,js實現(xiàn)繼承有以下通用的幾種方式
1. 使用對象冒充實現(xiàn)繼承(該種實現(xiàn)方式可以實現(xiàn)多繼承)
實現(xiàn)原理:讓父類的構(gòu)造函數(shù)成為子類的方法,然后調(diào)用該子類的方法,通過this關(guān)鍵字給所有的屬性和方法賦值
function Parent(firstname) { this.fname=firstname; this.age=40; this.sayAge=function() { console.log(this.age); } } function Child(firstname) { this.parent=Parent; this.parent(firstname); delete this.parent; this.saySomeThing=function() { console.log(this.fname); this.sayAge(); } } var mychild=new Child("李"); mychild.saySomeThing();
2. 采用call方法改變函數(shù)上下文實現(xiàn)繼承(該種方式不能繼承原型鏈,若想繼承原型鏈,則采用5混合模式)
實現(xiàn)原理:改變函數(shù)內(nèi)部的函數(shù)上下文this
,使它指向傳入函數(shù)的具體對象
function Parent(firstname) { this.fname=firstname; this.age=40; this.sayAge=function() { console.log(this.age); } } function Child(firstname) { this.saySomeThing=function() { console.log(this.fname); this.sayAge(); } this.getName=function() { return firstname; } } var child=new Child("張"); Parent.call(child,child.getName()); child.saySomeThing();
3. 采用Apply方法改變函數(shù)上下文實現(xiàn)繼承(該種方式不能繼承原型鏈,若想繼承原型鏈,則采用5混合模式)
實現(xiàn)原理:改變函數(shù)內(nèi)部的函數(shù)上下文this
,使它指向傳入函數(shù)的具體對象
function Parent(firstname) { this.fname=firstname; this.age=40; this.sayAge=function() { console.log(this.age); } } function Child(firstname) { this.saySomeThing=function() { console.log(this.fname); this.sayAge(); } this.getName=function() { return firstname; } } var child=new Child("張"); Parent.apply(child,[child.getName()]); child.saySomeThing();
4. 采用原型鏈的方式實現(xiàn)繼承
實現(xiàn)原理:使子類原型對象指向父類的實例以實現(xiàn)繼承,即重寫類的原型,弊端是不能直接實現(xiàn)多繼承
function Parent() { this.sayAge=function() { console.log(this.age); } } function Child(firstname) { this.fname=firstname; this.age=40; this.saySomeThing=function() { console.log(this.fname); this.sayAge(); } } Child.prototype=new Parent(); var child=new Child("張"); child.saySomeThing();
5. 采用混合模式實現(xiàn)繼承
function Parent() { this.sayAge=function() { console.log(this.age); } } Parent.prototype.sayParent=function() { alert("this is parentmethod!!!"); } function Child(firstname) { Parent.call(this); this.fname=firstname; this.age=40; this.saySomeThing=function() { console.log(this.fname); this.sayAge(); } } Child.prototype=new Parent(); var child=new Child("張"); child.saySomeThing(); child.sayParent();
更多關(guān)于JavaScript相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《javascript面向?qū)ο笕腴T教程》、《JavaScript錯誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學(xué)運算用法總結(jié)》
希望本文所述對大家JavaScript程序設(shè)計有所幫助。
相關(guān)文章
Bootstrap實現(xiàn)下拉菜單多級聯(lián)動
這篇文章主要為大家詳細(xì)介紹了Bootstrap實現(xiàn)下拉菜單多級聯(lián)動,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-119行javascript代碼獲取QQ群成員具體實現(xiàn)
22 行 JavaScript 代碼實現(xiàn) QQ 群成員提取器,如果沒有達(dá)到效果可能原因一是QQ版本升級了,二是博客里面的代碼也有些繁瑣2013-10-10JavaScript實現(xiàn)扯網(wǎng)動畫效果的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用JavaScript語言實現(xiàn)扯網(wǎng)動畫效果,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)JS有一定的幫助,需要的可以參考一下2022-06-06javaScript 刪除確認(rèn)實現(xiàn)方法小結(jié)
因為對于內(nèi)容的刪除是件很重要的事,所以一般的系統(tǒng)中,都需要刪除確認(rèn)一下,以免誤刪,具體的方法如下,大家可以參考下。2009-12-12