日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

js继承的实现(转载)

發(fā)布時(shí)間:2025/4/5 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 js继承的实现(转载) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

原文地址:http://yahaitt.iteye.com/blog/250338

js繼承有5種實(shí)現(xiàn)方式:?
1、繼承第一種方式:對象冒充?
? 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所指向的對象,?
??? //第二步:執(zhí)行this.method方法,即執(zhí)行Parent所指向的對象函數(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();

轉(zhuǎn)載于:https://www.cnblogs.com/davidwang456/archive/2013/04/19/3029890.html

總結(jié)

以上是生活随笔為你收集整理的js继承的实现(转载)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。