js繼承實(shí)現(xiàn)方法詳解
本文實(shí)例講述了js繼承實(shí)現(xiàn)方法。分享給大家供大家參考,具體如下:
var animal=function(name){ //構(gòu)造函數(shù)
this.name=name;
this.sayhello=function(){
alert("hi我是"+this.name);
};
}
animal.prototype.shout=function(){ //prototype主要作用:給類增加一個(gè)新的屬性或函數(shù)
alert(this.name+"正在叫!");
};
animal.prototype.game=function(){
alert(this.name+"正在玩耍!");
};
var dog=new animal("小黑"); //實(shí)例化
dog.sayhello();
dog.shout();
dog.game();
一、原型繼承
var animal=function(name){
this.name=name;
this.sayhello=function(){
alert("hi我是"+this.name);
};
}
animal.prototype.shout=function(){
alert(this.name+"正在叫!");
};
animal.prototype.game=function(){
alert(this.name+"正在玩耍!");
};
var Dog=function(name){
this.name=name;
this.shout=function(){ //重寫父類的函數(shù)
alert(this.name+"汪汪叫!");
}
}
Dog.prototype=Cat.prototype=new animal();
var dog=new Dog("小黑");
dog.sayhello();
dog.shout();
dog.game();
var cat=new Cat("小白");
cat.sayhello();
cat.shout();
cat.game();
animal是父類(或超類),要繼承于誰,誰就是父類(超類)
改進(jìn):專門寫個(gè)函數(shù),用來實(shí)現(xiàn)繼承
var animal=function(name){
this.name=name;
this.sayhello=function(){
alert("hi我是"+this.name);
};
}
Function.prototype.extends=function(superclass){ //extends是保留關(guān)鍵字,不能拿出來單獨(dú)使用,如var extends=function(){},而這里可以使用是因?yàn)樗鰹楹瘮?shù)的屬性添加上去
this.prototype=new superclass();
};
animal.prototype.shout=function(){
alert(this.name+"正在叫!");
};
animal.prototype.game=function(){
alert(this.name+"正在玩耍!");
};
var Dog=function(name){
this.name=name;
this.shout=function(){
alert(this.name+"汪汪叫!");
}
}
Dog.extends(animal);
var dog=new Dog("小黑");
dog.sayhello();
dog.shout();
dog.game();
var dog=new Dog("小白");
dog.sayhello();
dog.shout();
dog.game();
二、call,apply繼承(不完全繼承)
var animal=function(name){
this.name=name;
this.sayhello=function(){
alert("hi我是"+this.name);
};
}
animal.prototype.shout=function(){
alert(this.name+"正在叫!");
};
animal.prototype.game=function(){
alert(this.name+"正在玩耍!");
};
var Dog=function(name){
animal.call(this,name);
}
var dog=new Dog("小黑");
dog.sayhello();
dog.shout();
dog.game();
輸出:hi我是小黑
TypeError: dog.shout is not a function
通過call(apply)的方式,只能改變其this指向,通過prototype添加進(jìn)去的方法,他是無法直接繼承的
總結(jié):call,apply這種方式的繼承適用于沒有prototype的類或者不需要繼承prototype所添加屬性或函數(shù)的類,因?yàn)閏all和apply函數(shù)只是實(shí)現(xiàn)了方法的替換,而沒有對對象的屬性和函數(shù)進(jìn)行復(fù)制操作
原型繼承,則可以繼承所有的屬性和函數(shù)
繼承的屬性,只有刪除原有屬性,才會(huì)輸出繼承的屬性
更多關(guān)于JavaScript相關(guān)內(nèi)容可查看本站專題:《javascript面向?qū)ο笕腴T教程》、《JavaScript中json操作技巧總結(jié)》、《JavaScript切換特效與技巧總結(jié)》、《JavaScript查找算法技巧總結(jié)》、《JavaScript錯(cuò)誤與調(diào)試技巧總結(jié)》、《JavaScript數(shù)據(jù)結(jié)構(gòu)與算法技巧總結(jié)》、《JavaScript遍歷算法與技巧總結(jié)》及《JavaScript數(shù)學(xué)運(yùn)算用法總結(jié)》
希望本文所述對大家JavaScript程序設(shè)計(jì)有所幫助。
相關(guān)文章
詳解javascript立即執(zhí)行函數(shù)表達(dá)式IIFE
本文主要介紹了javascript立即執(zhí)行函數(shù)表達(dá)式IIFE的相關(guān)知識(shí)。具有很好的參考價(jià)值,下面跟著小編一起來看下吧2017-02-02
document.execCommand()的用法小結(jié)
本篇文章主要是對document.execCommand()的用法進(jìn)行了詳細(xì)的介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-01-01
js限制輸入框只能輸入數(shù)字(onkeyup觸發(fā))
這篇文章主要介紹了通過js實(shí)現(xiàn)input輸入框只能輸入數(shù)字的實(shí)現(xiàn)方法,主要是通過正則表達(dá)式替換實(shí)現(xiàn),需要的朋友可以參考下2018-09-09
TypeScript函數(shù)和類型斷言實(shí)例詳解
在某些情況下,我們會(huì)比TS更清楚一個(gè)數(shù)據(jù)的類型,這種時(shí)候我們就可以使用斷言來告訴TS相信我,我知道自己在干什么,下面這篇文章主要給大家介紹了關(guān)于TypeScript函數(shù)和類型斷言的相關(guān)資料,需要的朋友可以參考下2022-06-06
使用BootStrapValidator完成前端輸入驗(yàn)證
這篇文章主要為大家詳細(xì)介紹了使用BootStrapValidator來完成前端輸入驗(yàn)證,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
javascript高亮效果的二種實(shí)現(xiàn)方法
js高亮效果實(shí)現(xiàn)代碼,直接靜態(tài)頁面即可,不用每次都要生成2008-09-09

