js中繼承可以分為兩種:物件冒充和原型鏈方式
一、物件冒充包括三種:臨時屬性方式、call()、apply()方式
1.臨時屬性方式
複製代碼代碼如下:
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()方式
實質上是改變了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();
缺點:先來看這麼一張記憶體分配圖:
在OO概念中,new實例化後,物件就在堆記憶體中形成了自己的空間,值得注意的是,這個程式碼段。而成員方法就是存在這個程式碼段的,而且方法是共用的。問題就在這裡,透過物件冒充方式繼承時,所有的成員方法都是指向this的,也就是說new之後,每個實例將都會擁有這個成員方法,並不是共用的,這就造成了大量的內存浪費。且透過物件冒充的方式,無法繼承透過prototype方式定義的變數和方法,如以下程式碼將會出錯:
複製代碼代碼如下:
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')); //檢查是否為自己屬性
接下來按照上面的例子來理解以下js原型鏈概念:
原型鏈可以理解成:js中每個物件都有一個隱藏的__proto__屬性,一個實例化物件的__proto__屬性指向其類別的prototype方法,而這個prototype方法又可以被賦值成另一個實例化對象,這個對象的__proto__又需要指向其類,由此形成一條鏈,也就是前面程式碼中的
複製代碼代碼如下:
F2E.prototype = new Person()
這句話是關鍵。 js物件在讀取某個屬性時,會先尋找自身屬性,沒有則再去依序查找原型鏈上物件的屬性。也就是說原型鏈的方法是可以共用的,這樣就解決了物件冒充浪費記憶體的缺點。
下面再來來說缺點:
缺點顯而易見,原型連結方式繼承,就是實例化子類別時不能將參數傳給父類,也就是為什麼這個例子中function Person()沒有參數,而是直接寫成了this.name=”Simon」的原因。下面的程式碼將無法達到預期的效果:
複製代碼代碼如下:
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(); //此處無法進行傳值,this.name或name都不行,直接寫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();
最後,總結自認為較好的繼承實作方式,成員變數採用物件冒充方式,成員方法採用原型鏈方式,程式碼如下:
複製代碼代碼如下:
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();
//此處注意一個細節,showId不能寫在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();