js中比較好的繼承方式,js繼承方式
前面說到了原型和原型鏈,今天就來說說在物件導向中比較好的繼承方式吧。先來看看兩種基礎的繼承方式:
一、建構函式型
function People(name){this.name=name;}People.prototype.sayName=function(){console.log(this.name);}function Student(id,name){People.call(this,name);this.id=id;}Student.prototype.sayId=function(){console.log(this.id);}
在Student類裡用了Person的call方法,就會在對象上自動綁定了name屬性。但是這樣的方法的缺點就是無法繼承原型方法,也就是無法繼承sayName這個方法。
二、原型覆蓋型
function People(name){<span style="white-space:pre"></span>this.name=name;<span style="white-space:pre"></span>this.friends=[];}People.prototype.sayName=function(){<span style="white-space:pre"></span>console.log(this.name);}function Student(id){<span style="white-space:pre"></span>this.id=id;}Student.prototype=new People();Student.prototype.sayId=function(){<span style="white-space:pre"></span>console.log(this.id);}var stu1=new Student(1);stu1.name="stu1";console.log(stu1);在這裡,Student類的原型指向了People的一個執行個體,雖然這樣既繼承了屬性,又繼承了原型方法,但是缺點就是無法像建構函式那樣傳遞name進去,只能執行個體化以後再為name屬性賦值,大家都知道,雖然在尋找時遵循原型鏈的規則,但是在賦值時,這個規則卻是不實用的,執行個體化過後的對象是無法修改原型對象裡面的屬性的。比如上式裡面,為stu1的name屬性賦值了,但是結果如下:
正如大家所見,並沒有修改其原型對象的name屬性,而是在stu1上加了一條name屬性
再來看看修改對象指標:
function People(name){this.name=name;this.friends=[];}People.prototype.sayName=function(){console.log(this.name);}function Student(id){this.id=id;}Student.prototype=new People();Student.prototype.sayId=function(){console.log(this.id);}var stu1=new Student(1);stu1.friends=["ro"];console.log(stu1);
結果與一般類型的差不多,但是我們用下面這種方式:
function People(name){this.name=name;this.friends=[];}People.prototype.sayName=function(){console.log(this.name);}function Student(id){this.id=id;}Student.prototype=new People();Student.prototype.sayId=function(){console.log(this.id);}var stu1=new Student(1);stu1.friends.push("Bob");console.log(stu1);
大家看到了,在原型對象裡的friends數組加了一個Bob,那是因為friends是參考型別,找到指標後再為其push一個值,這樣就修改了原型對象的屬性,但是為其賦值的話就會在對象上增加一個,而不是修改原型對象,問題來了,這種push的方式能修改原型對象的friends,但是每一個Student對象都要擁有自己私人的friends屬性怎麼辦呢,可以為其賦值,但是這跟我們要的效果不一樣,要的就是friends以及name屬性都能成為私人屬性,但是方法是原型方法,那麼就能借鑒以上兩種方式。
function People(name,arr){<span style="white-space:pre"></span>this.name=name;<span style="white-space:pre"></span>this.friends=arr;}People.prototype.sayName=function(){<span style="white-space:pre"></span>console.log(this.name);}function Student(id,name,arr){<span style="white-space:pre"></span>People.call(this,name,arr);<span style="white-space:pre"></span>this.id=id;}Student.prototype=new People();Student.prototype.sayId=function(){<span style="white-space:pre"></span>console.log(this.id);}var stu1=new Student(1,"I",["bob"]);console.log(stu1);
大家看到了,name和friends屬性確實成為了私人屬性,但是原型對象裡面也有這兩個屬性,這樣也不好,佔用了空間,那應該怎麼辦呢?怎麼樣才能去掉這個原型對象裡面的屬性,其實很簡單,我們想要的只是People原型對象裡面的方法,那麼我們只需要繼承原型對象就行了呀
function People(name,arr){this.name=name;this.friends=arr;}People.prototype.sayName=function(){console.log(this.name);}function Student(id,name,arr){People.call(this,name,arr);this.id=id;}Student.prototype=People.prototype;Student.prototype.sayId=function(){console.log(this.id);}var stu1=new Student(1,"I",["bob"]);console.log(stu1);
這裡大概就變成我們想要的樣子了,但是還有一點問題,constructor指向問題,本來這個constructor應該指向Student,修改一下即可:
function inheritPrototype(subType, superType){ var prototype = superType.prototype;prototype.constructor = subType; subType.prototype = prototype; }function People(name,arr){this.name=name;this.friends=arr;}People.prototype.sayName=function(){console.log(this.name);}function Student(id,name,arr){People.call(this,name,arr);this.id=id;}inheritPrototype(Student,People);Student.prototype.sayId=function(){console.log(this.id);}var stu1=new Student(1,"I",["bob"]);console.log(stu1);
搞定了,這裡我在前面寫了一個繼承原型的函數,首先將子類的原型指向父類的原型,然後修改其constructor指向,貌似這樣就已經很完美了,但是在不知道不覺中我又發現了一個問題,我也是此時此刻發現的,所以寫部落格還是有非常大的協助的,可以幫自己理清思路,大家看到了我修改了constructor指向,但是我同時修改了父類的constructor指向,因為兩者的原型對象是一個對象,所以這是有問題的。那麼怎樣才能修正這個問題呢?其實動動腦筋就知道,我們需要複製一個People的原型的副本,然後將Student的原型指向這個副本,那麼問題來了,到底是深拷貝還是淺拷貝呢,至於深拷貝和淺拷貝的概念不知道的可以上網查一查,深拷貝是完全可以滿足這個要求的,那如果是淺拷貝呢,而且這個淺拷貝還不是普通的淺拷貝,要用到上面我們分析的知識,剛剛我們是為其賦值,賦值的話我們是不能修改一個對象的原型對象的。那麼問題就迎刃而解了,我們可以這樣:
function inheritPrototype(subType, superType){ function Obj(){}Obj.prototype=superType.prototype;var newObject=new Obj();newObject.constructor = subType; subType.prototype = newObject; }function People(name,arr){this.name=name;this.friends=arr;}People.prototype.sayName=function(){console.log(this.name);}function Student(id,name,arr){People.call(this,name,arr);this.id=id;}inheritPrototype(Student,People);Student.prototype.sayId=function(){console.log(this.id);}var stu1=new Student(1,"I",["bob"]);console.log(stu1);
在inheritPrototype函數裡,我先建立了一個類,然後將這個類的原型指向了父類的原型,然後再執行個體化一個對象,這時我為這個對象的constructor賦值,那麼這個constructor屬性就會存在於這個對象上,而不會去修改原型對象的constructor。唯一的缺點就是多了一重繼承。但是這是最好的繼承方式,我們來看看結果:
看上去就完美了,這就是比較好的繼承方式,寫到末尾,我想說寫部落格確實不錯,不僅協助了大家,也協助了自己,大家一起成長,一起進步!