Object.prototype. When an object gets request for a property, if it does not have one, it checks for it’s prototype’s prototype. The search goes till the
Object.prototype is reached.
The object instance keeps track of it’s prototype using [[Prototype]] property. That is, all new object instances point to the same prototype object, thereby reducing duplicating of code.
Following rules apply to prototypes:
- Object.getPrototypeOf() function determines the [[prototype]] of the object.
- __proto__ property allows us to read from and write to [[prototype]] property (to be added in ECMA Script 6).
- isPrototypeOf() method determines if an object is a prototype of another.
- Object.create() creates the object with specific prototype.
Advantages of using prototypes
This can be used to inherit the base class methods to child class without creating new instance of base class.
Prototype defines methods to all instances and new copy of method is not created along with the object. Hence memory is saved.
Disadvantages of using prototypes
Creating a method outside function body, inhibits them from accessing local private variables.