Prototypes and inheritance in javascript

Preview:

Citation preview

drakedrake

What is prototype ?

Wikipedia Say: …樣品 (prototype)是指某種新技術在投入量產之前的所作的模型,用以檢測產品質量,保障正常運行。在電子技術、機械工程、車輛工程等方面廣泛運用,實驗產品相應地被稱為樣機,樣車等。

Of Objects and Classes JavaScript is full of objects

Ex: Array in JavaScript is an object with values and also methods like push, reverse, and pop

Where does a method like push come from?

Of Prototypes Every object in JavaScript holds a hidden piece of

state How can we grab that reference ?

Object.getPrototypeOf(point) != Object.getPrototypeOf(myArray);

__proto__

What Makes Prototypes Special?

Example

Sharing Prototypes

Example

Of Functions Functions in JavaScript are objects Every function object in JavaScript has a prototypeprototype

property

Do not confuse this prototype property prototype property with the __proto__ __proto__ property – they do not serve the same purpose or point to the same object.

Imagine we want to create a new object and make the new object behave like an array

Constructor Functions The first letter of a constructor function is

capitalized by convention A constructor function expects to be used in

conjunction with the new operator to construct objects.

Reference http://msdn.microsoft.com/en-us/magazine/ff852808.aspx