国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > JavaScript > 正文

js實(shí)現(xiàn)繼承的5種方式

2019-11-20 11:06:30
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例講述了js實(shí)現(xiàn)繼承的5種方式。分享給大家供大家參考,具體如下:

1、繼承第一種方式:對(duì)象冒充

function Parent(username){  this.username = username;  this.hello = function(){   alert(this.username);  }}function Child(username,password){  //通過(guò)以下3行實(shí)現(xiàn)將Parent的屬性和方法追加到Child中,從而實(shí)現(xiàn)繼承  //第一步:this.method是作為一個(gè)臨時(shí)的屬性,并且指向Parent所指向的對(duì)象,  //第二步:執(zhí)行this.method方法,即執(zhí)行Parent所指向的對(duì)象函數(shù)  //第三步:銷(xiāo)毀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類(lèi)中的方法
call方法的第一個(gè)參數(shù)的值賦值給類(lèi)(即方法)中出現(xiàn)的this
call方法的第二個(gè)參數(shù)開(kāi)始依次賦值給類(lèi)(即方法)所接受的參數(shù)

function test(str){  alert(this.name + " " + str);}var object = new Object();object.name = "zhangsan";test.call(object,"langsin");//此時(shí),第一個(gè)參數(shù)值object傳遞給了test類(lèi)(即方法)中出現(xiàn)的this,而第二個(gè)參數(shù)"langsin"則賦值給了test類(lèi)(即方法)的strfunction 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ù)一樣,即賦值給類(lèi)(即方法)中出現(xiàn)的this
B、第二個(gè)參數(shù)為數(shù)組類(lèi)型,這個(gè)數(shù)組中的每個(gè)元素依次賦值給類(lèi)(即方法)所接受的參數(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、繼承的第四種方式:原型鏈方式,即子類(lèi)通過(guò)prototype將所有在父類(lèi)中通過(guò)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中將所有通過(guò)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);//將父類(lèi)的屬性繼承過(guò)來(lái)   this.world = world;//新增一些屬性 } Child.prototype = new Parent();//將父類(lèi)的方法繼承過(guò)來(lái) Child.prototype.sayWorld = function(){//新增一些方法   alert(this.world); } var c = new Child("zhangsan","lisi"); c.sayHello(); c.sayWorld();

希望本文所述對(duì)大家JavaScript程序設(shè)計(jì)有所幫助。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 高唐县| 通渭县| 奈曼旗| 东安县| 嘉善县| 孟州市| 庆阳市| 泗洪县| 黄梅县| 博客| 土默特右旗| 洪湖市| 潜山县| 阳春市| 定安县| 上饶市| 永兴县| 长海县| 玉门市| 闸北区| 城固县| 永嘉县| 邛崃市| 林甸县| 子长县| 亳州市| 嵊州市| 额尔古纳市| 会同县| 淄博市| 天津市| 肃北| 阿勒泰市| 肃南| 永定县| 琼海市| 呼伦贝尔市| 濮阳县| 正阳县| 长武县| 镇原县|