在學習JS的面向對象過程中,一直對constructor與prototype感到很迷惑,看了一些博客與書籍,覺得自己弄明白了,現在記錄如下:
我們都知道,在JS中有一個function的東西。一般人們叫它函數。比如下面的代碼
js代碼:
- function Person(name)
- {
- alert(name);
- }
- Person('js');//js
上面的代碼中,Person的表現的確跟一般的函數沒有什麼區別,接着看下面的代碼
- function Person(name)
- {
- this.name=name;
- this.showMe=function()
- {
- alert(this.name);
- }
- };
- var one=new Person('JavaScript');
- one.showMe();//JavaScript
很多人見到了久違的new操作符,於是就叫Person爲“類”,可是又沒有 關鍵字class的出現,覺得叫“類”有點勉強。於是退而求其次叫Person爲類的構造函數。這些概念好像都沒有錯,之所以出現這樣的情況,可能是因爲 大家都學習了傳統的面嚮對象語言(c++,c#,java等),還有一種思維定勢吧。爲了讓javascript也面向對象,要在javascript中 找到與傳統面嚮對象語言的影子。可是按照javascript的說法,function定義的這個Person就是一個Object(對象),而且還是一 個很特殊的對象,這個使用function定義的對象與使用new操作符生成的對象之間有一個重要的區別。這個區別就是function定義的對象有一個 prototype屬性,使用new生成的對象就沒有這個prototype屬性。
prototype屬性又指向了一個prototype對象,注意prototype屬性與prototype對象是兩個不同的東西,要注意區別。在prototype對象中又有一個constructor屬性,這個constructor屬性同樣指向一個constructor對象,而這個constructor對象恰恰就是這個function函數本身。
有點頭暈,看下圖吧:
不相信可以看下面的代碼:
- function Person(name)
- {
- this.name=name;
- this.showMe=function()
- {
- alert(this.name);
- }
- };
- var one=new Person('js');
- alert(one.prototype)//undefined
- alert(typeof Person.prototype);//object
- alert(Person.prototype.constructor);//function Person(name) {...};
上面的代碼證明了one這個對象沒有prototype屬性。
我們接着看代碼:
- function Person(name)
- {
- this.name=name;
- this.showMe=function()
- {
- alert(this.name);
- }
- };
- Person.prototype.from=function()
- {
- alert('I come from prototype.');
- }
- var one=new Person('js');
- one.showMe();//js,這個結果沒有什麼好奇怪的
- one.from();//I come from prototype.,這個結果有一點奇怪吧
要解釋這個結果就要仔細研究一下new這個操作符了.var one=new Person('js');這個語句執行的過程可以分成下面的語句:
- var one={};
- Person.call(one,'js');
按照《悟透javascript》書中說的,new形式創建對象的過程實際上可以分爲三步:
第一步是建立一個新對象(叫A吧);
第二步將該對象(A)內置的原型對象設置爲構造函數(就是Person)prototype 屬性引用的那個原型對象;
第三步就是將該對象(A)作爲this 參數調用構造函數(就是Person),完成成員設置等初始化工作。
其中第二步中出現了一個新名詞就是內置的原型對象,注意這個新名詞跟prototype對象不是一回事,爲了區別我叫它inobj,inobj就指向了函數Person的prototype對象。在person的prototype對象中出現的任何屬性或者函數都可以在one對象中直接使用,這個就是javascript中的原型繼承了。
又頭暈了,上圖吧!
這 樣one對象通過內置的原型對象inobj就可以直接訪問Person的prototype對象中的任何屬性與方法了。這也就解釋了上面的代碼中爲什麼 one可以訪問form函數了。因爲prototype對象中有一個constructor屬性,那麼one也可以直接訪問constructor屬性。
代碼:
- function Person(name)
- {
- this.name=name;
- this.showMe=function()
- {
- alert(this.name);
- }
- };
- Person.prototype.from=function()
- {
- alert('I come from prototype.');
- }
- var one=new Person('js');
- one.showMe();//js,這個結果沒有什麼好奇怪的
- one.from();//I come from prototype.,這個結果有一點奇怪吧
- alert(one.constructor);//function Person(name) {...}
- alert(Person.prototype.constructor);//function Person(name) {...}
接着看繼承是如何實現的。
- function Person(name)
- {
- this.name=name;
- this.showMe=function()
- {
- alert(this.name);
- }
- };
- Person.prototype.from=function()
- {
- alert('I come from prototype.');
- }
- function SubPerson()
- {
- }
- SubPerson.prototype=new Person();
- var subOne=new SubPerson();
- subOne.from();//I come from prototype.
- alert(subOne.constructor);//function Person(name) {...};
- alert(SubPerson.prototype.constructor);//function Person(name) {...};
繼承的實現很簡單,只需要把子類的prototype設置爲父類的一個對象即可。注意這裏說的可是對象哦!
那麼通過prototype屬性實現繼承的原理是什麼呢?還是先看圖形說明,然後編寫代碼進行驗證。
注意:紅色的方框就是把子類與父類鏈接起來的地方。這個就應該是傳說中的prototype鏈了吧。下面有代碼進行驗證。
js代碼:
- function Person(name)
- {
- this.name=name;
- this.showMe=function()
- {
- alert(this.name);
- }
- };
- Person.prototype.from=function()
- {
- alert('I come from prototype.');
- }
- var father=new Person('js');//爲了下面演示使用showMe方法,採用了js參數,實際多采用無參數
- alert(father.constructor);//查看構造函數,結果是:function Person(name) {...};
- function SubPer()
- {
- }
- SubPer.prototype=father;//注意這裏
- SubPer.prototype.constructor=SubPer;
- var son=new SubPer();
- son.showMe();//js
- son.from();//I come from prototype.
- alert(father.constructor);//function SubPer(){...}
- alert(son.constructor);//function SubPer(){...}
- alert(SubPer.prototype.constructor);//function SubPer(){...}
根據上圖的prototype鏈,還有代碼的結果,我想應該明白爲什麼使用prototype能夠實現
JS中的繼承了吧。