18
drake drake

Prototypes and inheritance in javascript

Embed Size (px)

Citation preview

Page 1: Prototypes and inheritance in javascript

drakedrake

Page 2: Prototypes and inheritance in javascript

What is prototype ?

Page 3: Prototypes and inheritance in javascript

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

Page 4: Prototypes and inheritance in javascript

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?

Page 5: Prototypes and inheritance in javascript

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__

Page 6: Prototypes and inheritance in javascript

What Makes Prototypes Special?

Page 7: Prototypes and inheritance in javascript

Example

Page 8: Prototypes and inheritance in javascript

Sharing Prototypes

Page 9: Prototypes and inheritance in javascript

Example

Page 10: Prototypes and inheritance in javascript

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.

Page 11: Prototypes and inheritance in javascript
Page 12: Prototypes and inheritance in javascript

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

Page 13: Prototypes and inheritance in javascript

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.

Page 14: Prototypes and inheritance in javascript
Page 15: Prototypes and inheritance in javascript
Page 16: Prototypes and inheritance in javascript
Page 17: Prototypes and inheritance in javascript
Page 18: Prototypes and inheritance in javascript

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