您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)JS怎么實(shí)現(xiàn)面向?qū)ο罄^承的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
JS是JavaScript的簡(jiǎn)稱(chēng),它是一種直譯式的腳本語(yǔ)言,其解釋器被稱(chēng)為JavaScript引擎,是瀏覽器的一部分,主要用于web的開(kāi)發(fā),可以給網(wǎng)站添加各種各樣的動(dòng)態(tài)效果,讓網(wǎng)頁(yè)更加美觀。
本文實(shí)例講述了JS實(shí)現(xiàn)面向?qū)ο罄^承的5種方式。分享給大家供大家參考,具體如下:
js是門(mén)靈活的語(yǔ)言,實(shí)現(xiàn)一種功能往往有多種做法,ECMAScript沒(méi)有明確的繼承機(jī)制,而是通過(guò)模仿實(shí)現(xiàn)的,根據(jù)js語(yǔ)言的本身的特性,js實(shí)現(xiàn)繼承有以下通用的幾種方式
1. 使用對(duì)象冒充實(shí)現(xiàn)繼承(該種實(shí)現(xiàn)方式可以實(shí)現(xiàn)多繼承)
實(shí)現(xiàn)原理:讓父類(lèi)的構(gòu)造函數(shù)成為子類(lèi)的方法,然后調(diào)用該子類(lèi)的方法,通過(guò)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ù)上下文實(shí)現(xiàn)繼承(該種方式不能繼承原型鏈,若想繼承原型鏈,則采用5混合模式)
實(shí)現(xiàn)原理:改變函數(shù)內(nèi)部的函數(shù)上下文this
,使它指向傳入函數(shù)的具體對(duì)象
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ù)上下文實(shí)現(xiàn)繼承(該種方式不能繼承原型鏈,若想繼承原型鏈,則采用5混合模式)
實(shí)現(xiàn)原理:改變函數(shù)內(nèi)部的函數(shù)上下文this
,使它指向傳入函數(shù)的具體對(duì)象
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. 采用原型鏈的方式實(shí)現(xiàn)繼承
實(shí)現(xiàn)原理:使子類(lèi)原型對(duì)象指向父類(lèi)的實(shí)例以實(shí)現(xiàn)繼承,即重寫(xiě)類(lèi)的原型,弊端是不能直接實(shí)現(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. 采用混合模式實(shí)現(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)于“JS怎么實(shí)現(xiàn)面向?qū)ο罄^承”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。