Posts

Showing posts with the label prototype

when to use prototypes

* For object creation optimization Built-in prototype object is if you'll be duplicating an object multiple times that will share common functionality. By attaching methods to the prototype, you can save on duplicating methods being created per each new instance. But when you attach a method to the prototype, all instances will have access to those methods * use prototypes if we need to declare a "non-static" method of the object. var myObject = function () { }; myObject.prototype.getA = function (){   alert("A"); }; myObject.getB = function (){   alert("B"); }; myObject.getB();  // This works fine myObject.getA();  // Error!

__proto__ vs prototype

The  __proto__  property of  Object.prototype  is an accessor property (a getter function and a setter function) that exposes the internal  [[Prototype]]  (either an object or  null ) of the object through which it is accessed. __proto__  is the actual object that is used in the lookup chain to resolve methods, etc.  prototype  is the object that is used to build  __proto__  when you create an object with  new : _proto_ var parent = {   color : 'white' } var child = {} child  = Object.create(parent) child.color //White parent.color = 'black' child.color  // 'black' because of _proto_ chain Prototype function   Soldier ( )   { } Soldier . prototype  =   {   weapons :   [ 'rifle' ,   'grenade' ,   'bayonet' ] ,   weaponOfChoice :   'rifle' } ; var  GIJoe  =   new   Soldier ( ) ; var  Ram...