Prototypes in JavaScript

Learn about the illusive prototype in JavaScript

JavaScript’s prototype object generates confusion wherever it goes. Seasoned JavaScript professionals, even authors frequently exhibit a limited understanding of the concept. Angus Croll believes a lot of the trouble stems from our earliest encounters with prototypes, which almost always relate to new, constructor and the very misleading prototype property attached to functions. In fact prototype is a remarkably simple concept.

 

The prototype property is initially an empty object, and can have members added to it – as you would any other object. In the snippet above, we’ve created a function, but if we call myObject(), it will simply return the window object, because it was defined within the global scope this will therefore return the global object, as it has not yet been instantiated.

Comments

Be the first to write a comment

You must me logged in to write a comment.