Search Topic Here

Monday, 17 August 2015

JavaScript Prototypes.



What is a prototype?

-->A prototype is an object from which other objects inherit properties.

-->The prototype property is initially an empty object, and can have members added to it - as you would any other object.



var myObject = function(name){
    this.name = name;
    return this;
};

console.log(typeof myObject.prototype); // object

myObject.prototype.getName = function(){
    return this.name;
};



the above code will simple return an object

Can any object be a prototype?

Yes.

Which objects have prototypes?

Every object has a prototype by default. Since prototypes are themselves objects, every prototype has a prototype too. (There is only one exception, the default object prototype at the top of every prototype chain. More on prototype chains later)



 what is an object ?

An object in JavaScript is any unordered collection of key-value pairs. If it’s not a primitive (undefined, null, boolean, number or string) it’s an object.


Secret property

Every object within JavaScript has a “secret” property added to it when it is defined or instantiated, named __proto__;
this is how the prototype chain is accessed. However, it is not a good idea to access __proto__ within your application,
as it is not available in all browsers.



What is the use of prototype:

If we are planing to design the application which needs a lot of operations in a single page (like canvas games,Mobile supported apps etc..) then we have to create alot of object instances in that case we need to go for prototype based programming..


No comments:

Post a Comment