js實(shí)現(xiàn)繼承的5種方式
本文實(shí)例講述了js實(shí)現(xiàn)繼承的5種方式。分享給大家供大家參考,具體如下:
1、繼承第一種方式:對(duì)象冒充
function Parent(username){ this.username = username; this.hello = function(){ alert(this.username); } } function Child(username,password){ //通過以下3行實(shí)現(xiàn)將Parent的屬性和方法追加到Child中,從而實(shí)現(xiàn)繼承 //第一步:this.method是作為一個(gè)臨時(shí)的屬性,并且指向Parent所指向的對(duì)象, //第二步:執(zhí)行this.method方法,即執(zhí)行Parent所指向的對(duì)象函數(shù) //第三步:銷毀this.method屬性,即此時(shí)Child就已經(jīng)擁有了Parent的所有屬性和方法 this.method = Parent; this.method(username);//最關(guān)鍵的一行 delete this.method; this.password = password; this.world = function(){ alert(this.password); } } var parent = new Parent("zhangsan"); var child = new Child("lisi","123456"); parent.hello(); child.hello(); child.world();
2、繼承第二種方式:call()方法方式
call方法是Function類中的方法
call方法的第一個(gè)參數(shù)的值賦值給類(即方法)中出現(xiàn)的this
call方法的第二個(gè)參數(shù)開始依次賦值給類(即方法)所接受的參數(shù)
function test(str){ alert(this.name + " " + str); } var object = new Object(); object.name = "zhangsan"; test.call(object,"langsin");//此時(shí),第一個(gè)參數(shù)值object傳遞給了test類(即方法)中出現(xiàn)的this,而第二個(gè)參數(shù)"langsin"則賦值給了test類(即方法)的str function Parent(username){ this.username = username; this.hello = function(){ alert(this.username); } } function Child(username,password){ Parent.call(this,username); this.password = password; this.world = function(){ alert(this.password); } } var parent = new Parent("zhangsan"); var child = new Child("lisi","123456"); parent.hello(); child.hello(); child.world();
3、繼承的第三種方式:apply()方法方式
apply方法接受2個(gè)參數(shù),
A、第一個(gè)參數(shù)與call方法的第一個(gè)參數(shù)一樣,即賦值給類(即方法)中出現(xiàn)的this
B、第二個(gè)參數(shù)為數(shù)組類型,這個(gè)數(shù)組中的每個(gè)元素依次賦值給類(即方法)所接受的參數(shù)
function Parent(username){ this.username = username; this.hello = function(){ alert(this.username); } } function Child(username,password){ Parent.apply(this,new Array(username)); this.password = password; this.world = function(){ alert(this.password); } } var parent = new Parent("zhangsan"); var child = new Child("lisi","123456"); parent.hello(); child.hello(); child.world();
4、繼承的第四種方式:原型鏈方式,即子類通過prototype將所有在父類中通過prototype追加的屬性和方法都追加到Child,從而實(shí)現(xiàn)了繼承
function Person(){ } Person.prototype.hello = "hello"; Person.prototype.sayHello = function(){ alert(this.hello); } function Child(){ } Child.prototype = new Person();//這行的作用是:將Parent中將所有通過prototype追加的屬性和方法都追加到Child,從而實(shí)現(xiàn)了繼承 Child.prototype.world = "world"; Child.prototype.sayWorld = function(){ alert(this.world); } var c = new Child(); c.sayHello(); c.sayWorld();
5、繼承的第五種方式:混合方式
混合了call方式、原型鏈方式
function Parent(hello){ this.hello = hello; } Parent.prototype.sayHello = function(){ alert(this.hello); } function Child(hello,world){ Parent.call(this,hello);//將父類的屬性繼承過來 this.world = world;//新增一些屬性 } Child.prototype = new Parent();//將父類的方法繼承過來 Child.prototype.sayWorld = function(){//新增一些方法 alert(this.world); } var c = new Child("zhangsan","lisi"); c.sayHello(); c.sayWorld();
希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。
相關(guān)文章
JavaScript函數(shù)apply()和call()用法與異同分析
這篇文章主要介紹了JavaScript函數(shù)apply()和call()用法與異同,結(jié)合實(shí)例形式分析了apply()和call()的功能、區(qū)別、使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2018-08-08小程序點(diǎn)餐界面添加購物車左右擺動(dòng)動(dòng)畫
這篇文章主要介紹了小程序點(diǎn)餐界面添加購物車左右擺動(dòng)動(dòng)畫,當(dāng)用戶點(diǎn)擊添加到購物車后會(huì)有一個(gè)左右擺動(dòng)的購物車提示效果,代碼簡單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2020-09-09基于JavaScript 性能優(yōu)化技巧心得(分享)
下面小編就為大家分享一篇基于JavaScript 性能優(yōu)化技巧心得,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-12-12javascript 動(dòng)態(tài)加載 css 方法總結(jié)
有時(shí)候我們?cè)谠O(shè)計(jì)網(wǎng)頁的時(shí)候想動(dòng)態(tài)的加載css文件,并不是將css文件寫死在頁面中,這時(shí)就可以使用下面方法.2009-07-07JavaScript使用Promise實(shí)現(xiàn)并發(fā)請(qǐng)求數(shù)限制
本文主要介紹了JavaScript使用Promise實(shí)現(xiàn)并發(fā)請(qǐng)求數(shù)限制,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04webpack.DefinePlugin與cross-env區(qū)別詳解
這篇文章主要介紹了webpack.DefinePlugin與cross-env區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02