1. 程式人生 > >JS中三種主要方法(函式定義)類別理解 —(JS面向物件&原型)

JS中三種主要方法(函式定義)類別理解 —(JS面向物件&原型)

JS中三種主要方法(函式定義)類別理解 —(JS面向物件&原型)

首先理解在JavaScript中:

  • 函式是“第一等公民”
  • 一切皆物件

javascript的方法可以分為三類:

  1. 類方法
  2. 物件方法
  3. 原型方法

程式碼示例:

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();

obj-A.func.call(obj-B)方法 ( 或 .apply( ) / .bind() ) 轉換呼叫該函式/方法的物件 (即函式/方法執行的作用域) 實現複用

(意思就是將obj-B看成obj-A呼叫func方法)

.call(), .apply(), .bind()

都是屬於 Function.prototype 原型的方法,它們是在JavaScript引擎內部實現的。因為屬於Function.prototype, 所以每個Function物件的例項都具有.call(), .apply(), .bind()方法屬性。

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的方法同名的方法會怎麼樣?

上程式碼示例:

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(), .apply(), .bind()

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的物件方法

最後,下面這個程式碼如果理解清晰,那麼這篇文章說的就已經理解了:

<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>