JS中對於prototype的理解

JS中的prototype是JS中比較難理解的一個部分javascript

本文基於下面幾個知識點:java

1 原型法設計模式

在.Net中可使用clone()來實現原型法面試

原型法的主要思想是,如今有1個類A,我想要建立一個類B,這個類是以A爲原型的,而且能進行擴展。咱們稱B的原型爲A。設計模式

2 javascript的方法能夠分爲三類:

a -> 類方法函數

b -> 對象方法測試

c -> 原型方法this

例子:spa

function People(name){  

    //對象屬性
    this.name=name; //對象方法 this.Introduce=function(){ alert("My name is "+this.name); } }

​//類靜態方法prototype

​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方法,原本調用的是obj1的func方法,可是,傳入obj後,改變了上下文對象,就經過obj對象來調用ojb1的方法了

好了,下面一個一個問題解決:

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的對象方法

最後,下面這個代碼若是理解清晰,那麼這篇文章說的就已經理解了:

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

 

4.最後跟你們介紹一下,call和prototype克隆的關係。

 function A(name){

  this.name = name;

}

A.prototype.sayName = function(){

  alert(this.name);

}

 function B(){}

B函數如何操做,才能夠繼承A的屬性和方法,有些同窗會說,new一個B的原型等於A的實例,也有人說經過call便可實現,剛開始我也是這麼理解的,後來我發現錯了。

function B(name){

  A.call(this,name);//此時,B函數只是繼承了A函數的屬性name,並未繼承A的sayName方法。

}

B.prototype = new A("Jack");//單獨這一句話,只能說明B函數克隆了A函數的sayName方法,所以call和prototype一塊兒使用才能繼承和克隆A函數的全部屬性和方法。

Call只會繼承對象自己的屬性和方法,而不會繼承原型中的方法。prototype自己的做用是克隆,而不是繼承。

對於這一點,對於初學者必定要注意,我去百度面試的時候,已經吃過這樣的虧了,下來以後,我寫了一個Demo,才發現我錯了,可是面試官也沒有告訴我是錯的。

相關文章
相關標籤/搜索