javascript是怎么繼承的介紹
更新時(shí)間:2012年01月05日 11:34:19 作者:
關(guān)于js中的繼承,網(wǎng)上有很多文章了,在這里我寫出自己對(duì)js中的繼承的理解
第一個(gè)階段:
function A(){
this.funB = function(){
alert('A:funB');
};
}
A.prototype = {
funA:function(){
alert('A:funA');
}
};
function B(){
}
function extend(sub,parent){
sub.prototype = new parent();
sub.prototype.constructor = sub;
}
extend(B,A);
var b = new B();
b.funA(); // out 'A:funA'
b.funB(); // out 'A:funB'
alert(b instanceof A); // out "true"
想必大家一眼就看出什么意思了,先是定義了A,B兩個(gè)類,然后使用extend方法來(lái)讓B繼承A類。extend的原理就是讓父類 new 到子類的prototype上。
用instanceof來(lái)檢測(cè)也為true,想要讓instanceof為true,那就必須兩個(gè)類的prototype對(duì)象要為同一個(gè)object,不管是間接或直接的。
這樣的方式有沒(méi)有問(wèn)題呢?在通常面向?qū)ο笳Z(yǔ)言中,子類在繼承父類時(shí),是不會(huì)觸發(fā)父類的構(gòu)造函數(shù)執(zhí)行,而這里是父類是在繼承時(shí)執(zhí)行的。
第二個(gè)階段
function A(){
this.Astr = 'hello A';
}
A.prototype = {
funA:function(){
alert(this.Astr);
}
};
function B(){
arguments.callee.superclass && arguments.callee.superclass.apply(this,arguments);
this.Bstr = 'hello B';
}
B.prototype = {
funB:function(){
alert(this.Bstr);
}
};
function C(){
arguments.callee.superclass && arguments.callee.superclass.apply(this,arguments);
alert(this.Astr);
alert(this.Bstr);
}
function extend(sub,parent){
var subproto = sub.prototype;
sub.prototype = parent.prototype;
typeof subproto != 'object' && (subproto = {});
typeof sub.prototype != 'object' && (sub.prototype = {});
for(var i in subproto){
sub.prototype[i] = subproto[i];
}
sub.superclass = parent;
}
//B 繼承 A
extend(B,A);
//C 繼承 B
extend(C,B);
var c = new C(); // out 'hello A','hello B'
c.funA(); //out 'hello A'
c.funB(); // out 'hello B'
alert(c instanceof A) // out true
alert(c instanceof B) // out true;
這里對(duì)extend方法做了一些改動(dòng),這里有個(gè)約定,每個(gè)子類都擁有一個(gè)superclass的屬性,用來(lái)引用她所繼承的父類,用一個(gè)空函數(shù)proto來(lái)獲得父類的prototype,實(shí)例化給子類的prototype,這樣就沒(méi)有執(zhí)行父類構(gòu)造器。
而是在子類的構(gòu)造器中用下來(lái)一段代碼來(lái)執(zhí)行約定要的父類構(gòu)造器。
arguments.callee.superclass && arguments.callee.superclass.apply(this,argumengs);
這樣就完成了類的繼承。
對(duì)于上面的代碼有沒(méi)有更方便的繼承寫法呢,修改Function的原型來(lái)看看:
Function.prototype.extend = function(parent){
var subproto = this.prototype;
this.prototype = parent.prototype;
typeof subproto != 'object' && (subproto = {});
typeof this.prototype != 'object' && (this.prototype = {});
for(var i in subproto){
this.prototype[i] = subproto[i];
}
this.superclass = parent;
return this;
}
function A(){
this.Astr = 'hello A';
}
A.prototype = {
funA:function(){
alert(this.Astr);
}
};
var B = function(){
arguments.callee.superclass && arguments.callee.superclass.apply(this,arguments);
this.Bstr = 'hello B';
}
B.prototype = {
funB:function(){
alert(this.Astr);
}
};
B.extend(A);
var C = function(){
arguments.callee.superclass && arguments.callee.superclass.apply(this,arguments);
alert(this.Astr);
alert(this.Bstr);
}.extend(B);
var c = new C(); // out 'hello A','hello B'
c.funA(); //out 'hello A'
c.funB(); // out 'hello B'
alert(c instanceof A) // out true
alert(c instanceof B) // out true;
這里的extend做的事情是: subproto引用子類的原prototype ,將子類的prototype 指向 父類的prototype對(duì)象,這樣就繼承了父類(這樣的目的是讓 子類實(shí)例 instanceof 父類 為 true)。然后歷遍subproto,將原prototype的成員添加到現(xiàn)prototype上,這樣子類重名的重名的成員就會(huì)覆蓋父類的成員。最后將子類的屬性superclass 指向 父類。
js繼承的關(guān)鍵就是保持原型鏈的唯一性,instanceof就以判斷實(shí)例的__proto__是否和父類的prototype為同一Object.
作者 cnblogs OD
復(fù)制代碼 代碼如下:
function A(){
this.funB = function(){
alert('A:funB');
};
}
A.prototype = {
funA:function(){
alert('A:funA');
}
};
function B(){
}
function extend(sub,parent){
sub.prototype = new parent();
sub.prototype.constructor = sub;
}
extend(B,A);
var b = new B();
b.funA(); // out 'A:funA'
b.funB(); // out 'A:funB'
alert(b instanceof A); // out "true"
想必大家一眼就看出什么意思了,先是定義了A,B兩個(gè)類,然后使用extend方法來(lái)讓B繼承A類。extend的原理就是讓父類 new 到子類的prototype上。
用instanceof來(lái)檢測(cè)也為true,想要讓instanceof為true,那就必須兩個(gè)類的prototype對(duì)象要為同一個(gè)object,不管是間接或直接的。
這樣的方式有沒(méi)有問(wèn)題呢?在通常面向?qū)ο笳Z(yǔ)言中,子類在繼承父類時(shí),是不會(huì)觸發(fā)父類的構(gòu)造函數(shù)執(zhí)行,而這里是父類是在繼承時(shí)執(zhí)行的。
第二個(gè)階段
復(fù)制代碼 代碼如下:
function A(){
this.Astr = 'hello A';
}
A.prototype = {
funA:function(){
alert(this.Astr);
}
};
function B(){
arguments.callee.superclass && arguments.callee.superclass.apply(this,arguments);
this.Bstr = 'hello B';
}
B.prototype = {
funB:function(){
alert(this.Bstr);
}
};
function C(){
arguments.callee.superclass && arguments.callee.superclass.apply(this,arguments);
alert(this.Astr);
alert(this.Bstr);
}
function extend(sub,parent){
var subproto = sub.prototype;
sub.prototype = parent.prototype;
typeof subproto != 'object' && (subproto = {});
typeof sub.prototype != 'object' && (sub.prototype = {});
for(var i in subproto){
sub.prototype[i] = subproto[i];
}
sub.superclass = parent;
}
//B 繼承 A
extend(B,A);
//C 繼承 B
extend(C,B);
var c = new C(); // out 'hello A','hello B'
c.funA(); //out 'hello A'
c.funB(); // out 'hello B'
alert(c instanceof A) // out true
alert(c instanceof B) // out true;
這里對(duì)extend方法做了一些改動(dòng),這里有個(gè)約定,每個(gè)子類都擁有一個(gè)superclass的屬性,用來(lái)引用她所繼承的父類,用一個(gè)空函數(shù)proto來(lái)獲得父類的prototype,實(shí)例化給子類的prototype,這樣就沒(méi)有執(zhí)行父類構(gòu)造器。
而是在子類的構(gòu)造器中用下來(lái)一段代碼來(lái)執(zhí)行約定要的父類構(gòu)造器。
復(fù)制代碼 代碼如下:
arguments.callee.superclass && arguments.callee.superclass.apply(this,argumengs);
這樣就完成了類的繼承。
對(duì)于上面的代碼有沒(méi)有更方便的繼承寫法呢,修改Function的原型來(lái)看看:
復(fù)制代碼 代碼如下:
Function.prototype.extend = function(parent){
var subproto = this.prototype;
this.prototype = parent.prototype;
typeof subproto != 'object' && (subproto = {});
typeof this.prototype != 'object' && (this.prototype = {});
for(var i in subproto){
this.prototype[i] = subproto[i];
}
this.superclass = parent;
return this;
}
function A(){
this.Astr = 'hello A';
}
A.prototype = {
funA:function(){
alert(this.Astr);
}
};
var B = function(){
arguments.callee.superclass && arguments.callee.superclass.apply(this,arguments);
this.Bstr = 'hello B';
}
B.prototype = {
funB:function(){
alert(this.Astr);
}
};
B.extend(A);
var C = function(){
arguments.callee.superclass && arguments.callee.superclass.apply(this,arguments);
alert(this.Astr);
alert(this.Bstr);
}.extend(B);
var c = new C(); // out 'hello A','hello B'
c.funA(); //out 'hello A'
c.funB(); // out 'hello B'
alert(c instanceof A) // out true
alert(c instanceof B) // out true;
這里的extend做的事情是: subproto引用子類的原prototype ,將子類的prototype 指向 父類的prototype對(duì)象,這樣就繼承了父類(這樣的目的是讓 子類實(shí)例 instanceof 父類 為 true)。然后歷遍subproto,將原prototype的成員添加到現(xiàn)prototype上,這樣子類重名的重名的成員就會(huì)覆蓋父類的成員。最后將子類的屬性superclass 指向 父類。
js繼承的關(guān)鍵就是保持原型鏈的唯一性,instanceof就以判斷實(shí)例的__proto__是否和父類的prototype為同一Object.
作者 cnblogs OD
您可能感興趣的文章:
- 基于JavaScript實(shí)現(xiàn)繼承機(jī)制之構(gòu)造函數(shù)方法對(duì)象冒充的使用詳解
- 基于JavaScript實(shí)現(xiàn)繼承機(jī)制之調(diào)用call()與apply()的方法詳解
- 基于JavaScript實(shí)現(xiàn)繼承機(jī)制之原型鏈(prototype chaining)的詳解
- Javascript中 關(guān)于prototype屬性實(shí)現(xiàn)繼承的原理圖
- JavaScript對(duì)象創(chuàng)建及繼承原理實(shí)例解剖
- 關(guān)于JavaScript的面向?qū)ο蠛屠^承有利新手學(xué)習(xí)
- javascript繼承之為什么要繼承
- Javascript繼承(上)——對(duì)象構(gòu)建介紹
- 淺談javascript的原型繼承
- 關(guān)于JavaScript中原型繼承中的一點(diǎn)思考
- JavaScript面向?qū)ο笾甈rototypes和繼承
- javascript類式繼承新的嘗試
- javascrip關(guān)于繼承的小例子
相關(guān)文章
layui 解決form表單點(diǎn)擊無(wú)反應(yīng)的問(wèn)題
今天小編就為大家分享一篇layui 解決form表單點(diǎn)擊無(wú)反應(yīng)的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10
HTML+JavaScript實(shí)現(xiàn)掃雷小游戲
這篇文章主要為大家詳細(xì)介紹了HTML+JavaScript實(shí)現(xiàn)掃雷小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09
javascript代碼在ie8里報(bào)錯(cuò) document.getElementById(...) 為空或不是對(duì)象的解決方
今天更升級(jí)了ie8,發(fā)現(xiàn)原來(lái)在ie7下可以運(yùn)行的代碼,不能運(yùn)行了,發(fā)現(xiàn)了一些細(xì)節(jié),附臨時(shí)修改辦法。2009-11-11
JavaScript捕捉事件和阻止冒泡事件實(shí)例分析
這篇文章主要介紹了JavaScript捕捉事件和阻止冒泡事件,結(jié)合實(shí)例形式分析了冒泡的原理及javascript阻止冒泡的相關(guān)操作技巧,需要的朋友可以參考下2018-08-08
JS DOM實(shí)現(xiàn)鼠標(biāo)滑動(dòng)圖片效果
這篇文章主要為大家詳細(xì)介紹了JS DOM實(shí)現(xiàn)鼠標(biāo)滑動(dòng)圖片效果,只要將鼠標(biāo)放上該商品的區(qū)域,原本折疊起來(lái)的商品便會(huì)自動(dòng)展開,感興趣的小伙伴們可以參考一下2016-03-03
JS實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)與刷新的方法匯總
這篇文章主要給大家介紹了關(guān)于JS實(shí)現(xiàn)頁(yè)面跳轉(zhuǎn)與刷新的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用JS具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
JavaScript 動(dòng)態(tài)添加表格行 使用模板、標(biāo)記
在客戶端使用JavaScript動(dòng)態(tài)添加表格行,先到網(wǎng)上找了相關(guān)的資料,發(fā)現(xiàn)有現(xiàn)成做好的組件,發(fā)現(xiàn)它只能夠滿足比較簡(jiǎn)單的要求。2009-10-10

