Saturday, November 10, 2012

JavaScript prototyping

In JavaScript, every object has a prototype property. Prototype property is also an object. All JavaScript objects inherit there properties and methods from there prototypes.

Using prototypes makes object creation faster.



When an inherited function is executed, the value of this points to the inheriting object, not to the prototype object where the function is an own property.

Object.create :  Create a new object with specified prototype object and properties

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Inheritance_and_the_prototype_chain

hasOwnProperty is an own property of Object.prototype. When we create an object, it inherits hasOwnProperty from Object.prototype.

Arrays inherits from Array.prototype (it has methods like forEach, call)
// f ---> Function.prototype ---> Object.prototype ---> null
 
 
The lookup time for properties that are high up on the prototype chain can have a negative impact on performance, and this may be significant in code where performance is critical. Additionally, trying to access nonexistent properties will always traverse the full prototype chain.

Also, when iterating over the properties of an object, every property that is on the prototype chain will be enumerated.



0 comments:

Post a Comment

Powered by Blogger.


Software Architect at Surge Global/ Certified Scrum Master

Experienced in Product Design, Software Engineering, Team management and Practicing Agile methodologies.

Search This Blog

Facebook