DIV CSS 佈局教程網

深入 JavaScript 的 prototype:從原型設計模式到屬性擴充
編輯:關於JavaScript     

prototype 大概是 JavaScript 裡最容易讓人卡住的概念之一。本文用幾個關鍵知識點,搭配可實際執行的實驗程式碼,把它徹底講清楚。

一、原型設計模式是什麼

在 .Net 中可以用 clone() 來實現原型法。原型法的主要思想是:已經有一個類別 A,我想建立一個類別 B,讓 B 以 A 為原型並能進行擴充,我們就稱 B 的原型是 A。

二、JavaScript 的方法分三類

a 類方法
b 物件方法
c 原型方法

用一個例子把三者放在一起對照:

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

另外要記住 obj1.func.call(obj) 的含義:把 obj 當成 obj1 來呼叫 func 方法。

三、prototype 到底是什麼

JavaScript 中每個物件都有 prototype 屬性,官方說法是「返回物件類型原型的引用」。理解 prototype 時,不要把它和繼承混為一談。

 A.prototype = new B();

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,接著定義 extendClass,並打算以 baseClass 的實例作為原型,讓 extendClass 也擁有 showMsg 這個物件方法。extendClass.prototype = new 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 不會覆蓋掉同名的方法。

六、實驗三:用 call 借用父層方法

那麼,如果想用 extendClass 的實例去呼叫 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 的物件方法,所以必須透過實例。

七、綜合驗證

如果下面這段程式碼你看懂了,那這篇文章要講的觀念就通了:

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

補充:prototype 的定義與常用操作

函數:原型

每一個建構函式都有一個屬性叫做 prototype。這個屬性非常有用:可以為某個特定類別宣告通用的變數或函式。

prototype 的定義

你不需要明確宣告 prototype,因為每個建構函式裡它都存在:

 function Test() {
}
alert(Test.prototype); // 輸出 "Object"

給 prototype 添加屬性

prototype 是一個物件,因此你可以為它添加屬性。添加上去的屬性,會成為所有以該建構函式建立的物件所共有的屬性。例如我有一個 Fish 型別,希望所有的魚都具備 livesIn="water" 和 price=20:

 function Fish(name, color) {
  this.name=name;
  this.color=color;
}
Fish.prototype.livesIn="water";
Fish.prototype.price=20;

接著造幾條魚:

 var fish1=new Fish("mackarel", "gray");
var fish2=new Fish("goldfish", "orange");
var fish3=new Fish("salmon", "white");

再看看牠們有哪些屬性:

 for (int i=1; i<=3; i++) {
  var fish=eval_r("fish"+i);
  // 我只是取得指向這條魚的指針
  alert(fish.name+","+fish.color+","+fish.livesIn+","+fish.price);
}

輸出結果:

 "mackarel, gray, water, 20"
"goldfish, orange, water, 20"
"salmon, white water, 20"

所有的魚都具備 livesIn 與 price,而我們從未為每條魚個別宣告過。原因是:當物件被建立時,建構函式會把自己的 prototype 指派給新物件的內部屬性 __proto__,這個 __proto__ 就是物件用來查找屬性的依據。

用 prototype 給物件添加函數

 function Employee(name, salary) {
  this.name=name;
  this.salary=salary;
}
Employee.prototype.getSalary=function getSalaryFunction() {
  return this.salary;
}
Employee.prototype.addSalary=function addSalaryFunction(addition) {
  this.salary=this.salary+addition;
}

可以照平常的方式建立物件:

 var boss1=new Employee("Joan", 200000);
var boss2=new Employee("Kim", 100000);
var boss3=new Employee("Sam", 150000);

並驗證它:

 alert(boss1.getSalary()); // 輸出 200000
alert(boss2.getSalary()); // 輸出 100000
alert(boss3.getSalary()); // 輸出 150000

下圖說明 prototype 的運作方式:物件的每個實例(boss1、boss2、boss3)都有一個內部屬性 __proto__,指向其建構式(Employee)的 prototype。當你執行 getSalary 或 addSalary 時,物件會在 __proto__ 中找到並執行這段程式碼。請特別注意:這裡並沒有複製程式碼本身,這正是原型式共享方法的最大價值——不管建立幾千幾萬個實例,方法只存在一份。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved