js的2種繼承方式詳解
js中繼承可以分為兩種:對(duì)象冒充和原型鏈方式
一、對(duì)象冒充包括三種:臨時(shí)屬性方式、call()及apply()方式
1.臨時(shí)屬性方式
function Person(name){
this.name = name;
this.say = function(){
alert('My name is '+this.name);
}
}
function F2E(name,id){
this.temp = Person;
this.temp(name);
delete this.temp;
this.id = id;
this.showId = function(){
alert('Good morning,Sir,My work number is '+this.id);
}
}
var simon = new F2E('Simon',9527);
simon.say();
simon.showId();
2.call()/apply()方式
實(shí)質(zhì)上是改變了this指針的指向
function Person(name){
this.name = name;
this.say = function(){
alert('My name is '+this.name);
}
}
function F2E(name,id){
Person.call(this,name); //apply()方式改成Person.apply(this,new Array(name));
this.id = id;
this.showId = function(){
alert('Good morning,Sir,My work number is '+this.id);
}
}
var simon = new F2E('Simon',9527);
simon.say();
simon.showId();
缺點(diǎn):先來(lái)看這么一張內(nèi)存分配圖:
![]() |
在OO概念中,new實(shí)例化后,對(duì)象就在堆內(nèi)存中形成了自己的空間,值得注意的是,這個(gè)代碼段。而成員方法就是存在這個(gè)代碼段的,并且方法是共用的。問(wèn)題就在這里,通過(guò)對(duì)象冒充方式繼承時(shí),所有的成員方法都是指向this的,也就是說(shuō)new之后,每個(gè)實(shí)例將都會(huì)擁有這個(gè)成員方法,并不是共用的,這就造成了大量的內(nèi)存浪費(fèi)。并且通過(guò)對(duì)象冒充的方式,無(wú)法繼承通過(guò)prototype方式定義的變量和方法,如以下代碼將會(huì)出錯(cuò):
function Person(name){
this.name = name;
this.say = function(){
alert('My name is '+this.name);
}
}
Person.prototype.age = 20;
Person.prototype.sayAge = function(){alert('My age is '+this.age)};
function F2E(name,id){
Person.apply(this,new Array(name));
this.id = id;
this.showId = function(){
alert('Good morning,Sir,My work number is '+this.id);
}
}
var simon = new F2E('Simon',9527);
simon.sayAge(); //提示TypeError: simon.sayAge is not a function
二、原型鏈方式
function Person(){
this.name = 'Simon';
}
Person.prototype.say = function(){
alert('My name is '+this.name);
}
function F2E(id){
this.id = id;
this.showId = function(){
alert('Good morning,Sir,My work number is '+this.id);
}
}
F2E.prototype = new Person();
var simon = new F2E(9527);
simon.say();
simon.showId();
alert(simon.hasOwnProperty('id')); //檢查是否為自身屬性
接下來(lái)按照上面的例子來(lái)理解以下js原型鏈概念:
![]() |
原型鏈可以理解成:js中每個(gè)對(duì)象均有一個(gè)隱藏的__proto__屬性,一個(gè)實(shí)例化對(duì)象的__proto__屬性指向其類(lèi)的prototype方法,而這個(gè)prototype方法又可以被賦值成另一個(gè)實(shí)例化對(duì)象,這個(gè)對(duì)象的__proto__又需要指向其類(lèi),由此形成一條鏈,也就是前面代碼中的
F2E.prototype = new Person()
這句是關(guān)鍵。js對(duì)象在讀取某個(gè)屬性時(shí),會(huì)先查找自身屬性,沒(méi)有則再去依次查找原型鏈上對(duì)象的屬性。也就是說(shuō)原型鏈的方法是可以共用的,這樣就解決了對(duì)象冒充浪費(fèi)內(nèi)存的缺點(diǎn)。
下面再來(lái)說(shuō)缺點(diǎn):
缺點(diǎn)顯而易見(jiàn),原型鏈方式繼承,就是實(shí)例化子類(lèi)時(shí)不能將參數(shù)傳給父類(lèi),也就是為什么這個(gè)例子中function Person()沒(méi)有參數(shù),而是直接寫(xiě)成了this.name=”Simon”的原因。下面的代碼將不能達(dá)到預(yù)期的效果:
function Person(name){
this.name = name;
}
Person.prototype.say = function(){
alert('My name is '+this.name);
}
function F2E(name,id){
this.id = id;
this.showId = function(){
alert('Good morning,Sir,My work number is '+this.id);
}
}
F2E.prototype = new Person();
var simon = new F2E("Simon",9527);
simon.say();
simon.showId();
function Person(name){
this.name = name;
}
Person.prototype.say = function(){
alert('My name is '+this.name);
}
function F2E(name,id){
this.id = id;
this.showId = function(){
alert('Good morning,Sir,My work number is '+this.id);
}
}
F2E.prototype = new Person(); //此處無(wú)法進(jìn)行傳值,this.name或者name都不行,直接寫(xiě)F2E.prototype = new Person('wood')是可以的,但是這樣的話simon.say()就變成了My name is wood
var simon = new F2E("Simon",9527);
simon.say(); //彈出 My name is undefined
simon.showId();
最后,總結(jié)一下自認(rèn)為較好的繼承實(shí)現(xiàn)方式,成員變量采用對(duì)象冒充方式,成員方法采用原型鏈方式,代碼如下:
function Person(name){
this.name = name;
}
Person.prototype.say = function(){
alert('My name is '+this.name);
}
function F2E(name,id){
Person.call(this,name);
this.id = id;
}
F2E.prototype = new Person();
//此處注意一個(gè)細(xì)節(jié),showId不能寫(xiě)在F2E.prototype = new Person();前面
F2E.prototype.showId = function(){
alert('Good morning,Sir,My work number is '+this.id);
}
var simon = new F2E("Simon",9527);
simon.say();
simon.showId();
相關(guān)文章
低門(mén)檻開(kāi)發(fā)iOS、Android、小程序應(yīng)用的前端框架詳解
結(jié)合AVM官網(wǎng)的介紹和我自己的一些實(shí)踐經(jīng)驗(yàn),我總結(jié)了一系列AVM的特性,我想這些內(nèi)容足以讓你主動(dòng)去學(xué)習(xí)AVM框架了2021-10-10Javascript遞歸打印Document層次關(guān)系實(shí)例分析
這篇文章主要介紹了Javascript遞歸打印Document層次關(guān)系的方法,實(shí)例分析了javascript中Document的層次關(guān)系,需要的朋友可以參考下2015-05-05jquery插件jquery.confirm彈出確認(rèn)消息
這篇文章介紹了插件jquery.confirm彈出確認(rèn)消息的實(shí)現(xiàn)方法,感興趣的小伙伴們可以參考一下2015-12-12使用JS進(jìn)行目錄上傳(相當(dāng)于批量上傳)
腳本使用了WScript.Shell和Scripting.FileSystemObject的組件,所以必須要在IE下面和打開(kāi)安全選項(xiàng)中運(yùn)行; 另外還用到了Jquery. 代碼只是客戶端代碼, 至于服務(wù)器的接收代碼網(wǎng)上好多了2010-12-12JavaScript返回0-1之間隨機(jī)數(shù)的方法
這篇文章主要介紹了JavaScript返回0-1之間隨機(jī)數(shù)的方法,涉及javascript中Math對(duì)象random方法的使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04