Sunday, October 16, 2011

Learning javascript: understanding prototype property

Prototype in javascript is so important, because javascript implements the object inheritance via prototype delegation. Here I tried to summarize what I found for the javascript property. The examples I used are tested on Firefox Javscript shell .
Please note that in Javascript function is also an object, so here function and function object are interchangeable.

1. Every function object is automatically created with its own prototype property, the prototype property itself is an javascript object.You can add property or functions to the prototype property via function object.
2. When a object is created by using new keyword, it will get the prototype object from the constructor function;  created object can not access prototype property directly like function object, but it can access the property of the prototype object:

3. When the created object instance read a property of the prototype, which is directly linked to the prototype object of its function constructor; while you want to modify the property, you end up by creating a new property in the object itself, which will not impact the same property name in the original function prototype. I believe javascript in this way implements the OO inheritance and overriding.

4. The function object prototype property, which itself is an object, its default value contains only one hidden property: constructor, whose value is the same as the function itself.

No comments:

Post a Comment