Javascript語言非常重要的一個特徵是:Javascript是基於原型的語言(prototype-based language),而C++、Object Pascal、C#、Java是居於類的語言(class-based language);另外一個重要特性是Javascript是動態語言,而C++等是靜態語言(C#在4.0版本也引入了一些動態特性)。
基於類的語言(Java) |
基於原型的語言 (JavaScript) |
類和執行個體是獨立的實體概念。 |
所有的對象都是執行個體。 |
使用類的定義語句來定義一個類;使用建構函式來執行個體化一個類 |
使用建構函式來定義和建立一系列對象 |
使用 new 操作符來建立一個對象 |
相同 |
使用類的定義語句來定義一個已存在類的子類,然後建立對象的層級關係。 |
使用建構函式類建立一個對象,通過指派原形化的對象並使其關聯於已存在的建構函式Construct an object hierarchy by assigning an object as the prototype associated with a constructor function. |
Inherit properties by following the class chain. 通過類來繼承屬性 |
Inherit properties by following the prototype chain. 通過原型來繼承屬性 |
Class definition specifies all properties of all instances of a class. Cannot add properties dynamically at run time. 類定義了執行個體的所有屬性,不能在運行時動態添加 |
Constructor function or prototype specifies an initial set of properties. Can add or remove properties dynamically to individual objects or to the entire set of objects. 建構函式或者原型定義了屬性的原創組合,可以在運行時動態添加刪除屬性 |
參考:
http://en.wikipedia.org/wiki/Prototype_pattern
http://en.wikipedia.org/wiki/Prototype-based
http://en.wikipedia.org/wiki/JavaScript
Class-Based vs. Prototype-Based Languages