JS中的phototype是JS中比較難理解的一個部分javascript
本文基於下面幾個知識點:java
1 原型法設計模式node
在.Net中可使用clone()來實現原型法設計模式
原型法的主要思想是,如今有1個類A,我想要建立一個類B,這個類是以A爲原型的,而且能進行擴展。咱們稱B的原型爲A。數組
2 javascript的方法能夠分爲三類:函數
a 類方法測試
b 對象方法this
c 原型方法prototype
例子:設計
function People(name) { this.name=name; //對象方法 this.Introduce=function(){ alert("My name is "+this.name); } } //類方法 People.Run=function(){ alert("I can run"); } //原型方法 People.prototype.IntroduceChinese=function(){ alert("個人名字是"+this.name); } //測試 var p1=new People("Windking"); p1.Introduce(); People.Run(); p1.IntroduceChinese();
3 obj1.func.call(obj)方法
意思是將obj當作obj1,調用func方法
好了,下面一個一個問題解決:
prototype是什麼含義?
javascript中的每一個對象都有prototype屬性,Javascript中對象的prototype屬性的解釋是:返回對象類型原型的引用。
A.prototype = new B();
理解prototype不該把它和繼承混淆。A的prototype爲B的一個實例,能夠理解A將B中的方法和屬性所有克隆了一遍。A能使用B的方法和屬性。這裏強調的是克隆而不是繼承。能夠出現這種狀況:A的prototype是B的實例,同時B的prototype也是A的實例。
先看一個實驗的例子:
function baseClass() { this.showMsg = function() { alert("baseClass::showMsg"); } } function extendClass() { } extendClass.prototype = new baseClass(); var instance = new extendClass(); instance.showMsg(); // 顯示baseClass::showMsg
咱們首先定義了baseClass類,而後咱們要定義extentClass,可是咱們打算以baseClass的一個實例爲原型,來克隆的extendClass也同時包含showMsg這個對象方法。
extendClass.prototype = new baseClass()就能夠閱讀爲:extendClass是以baseClass的一個實例爲原型克隆建立的。
那麼就會有一個問題,若是extendClass中自己包含有一個與baseClass的方法同名的方法會怎麼樣?
下面是擴展實驗2:
function baseClass() { this.showMsg = function() { alert("baseClass::showMsg"); } } function extendClass() { this.showMsg =function () { alert("extendClass::showMsg"); } } extendClass.prototype = new baseClass(); var instance = new extendClass(); instance.showMsg();//顯示extendClass::showMsg
實驗證實:函數運行時會先去本體的函數中去找,若是找到則運行,找不到則去prototype中尋找函數。或者能夠理解爲prototype不會克隆同名函數。
那麼又會有一個新的問題:
若是我想使用extendClass的一個實例instance調用baseClass的對象方法showMsg怎麼辦?
答案是可使用call:
extendClass.prototype = new baseClass(); var instance = new extendClass(); var baseinstance = new baseClass(); baseinstance.showMsg.call(instance);//顯示baseClass::showMsg
這裏的baseinstance.showMsg.call(instance);閱讀爲「將instance當作baseinstance來調用,調用它的對象方法showMsg」
好了,這裏可能有人會問,爲何不用baseClass.showMsg.call(instance);
這就是對象方法和類方法的區別,咱們想調用的是baseClass的對象方法
------------------------------------------------------------------------------------------------------------------------------------------------------------------
對象方法和類方法的區別:
一、對象方法理解就很簡單了,主要是若是類生成一個實例,那麼該實例就能使用該方法
二、類方法,不須要經過生成實例就可使用的方法
三、原型方法主要是用來對JS已有的系統對象進行擴展而生的,例如Array數組沒有什麼方法,你能夠爲其增長原型方法,那麼建立的數組就擁有了該方法。
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
一、對象方法包括構造函數中的方法以及構造函數原型上面的方法;
二、類方法,其實這裏的類就是一個函數,在js中因爲函數也是一個對象,因此能夠爲函數添加屬性以及方法,這種方法在node中用的比較多;
三、原型方法通常用於對象實例共享,好比Person.prototype.sayName=function(){console.log(this.name);};在原型上面添加該方法,就能實現共享。這樣就不用每一次初始化一個實例的時候,爲其分配相應的內存了。
------------------------------------------------------------------------------------------------------------------------------------------------------------------
最後,下面這個代碼若是理解清晰,那麼這篇文章說的就已經理解了:
<script type="text/javascript"> function baseClass() { this.showMsg = function() { alert("baseClass::showMsg"); } this.baseShowMsg = function() { alert("baseClass::baseShowMsg"); } } baseClass.showMsg = function() { alert("baseClass::showMsg static"); } function extendClass() { this.showMsg =function () { alert("extendClass::showMsg"); } } extendClass.showMsg = function() { alert("extendClass::showMsg static") } extendClass.prototype = new baseClass(); var instance = new extendClass(); instance.showMsg(); //顯示extendClass::showMsg instance.baseShowMsg(); //顯示baseClass::baseShowMsg instance.showMsg(); //顯示extendClass::showMsg baseClass.showMsg.call(instance);//顯示baseClass::showMsg static var baseinstance = new baseClass(); baseinstance.showMsg.call(instance);//顯示baseClass::showMsg </script>