JavaScript · Programming

Javascript prototype object

  1. Prototype is a builtin js property that is part of JS objects. It was introduced in JS version 1.1.
  2. Prototype object can be added only to Function,String, Image, Number, Date and Array.
  3. We can use protoype on the base class not on the objects derived from it.
  4. The prototype object help you quickly add a custom method or member to an object that is reflected on ALL instances of it.
  5. Prototype object can only be used to add PUBLIC member and methods. It cannot add private members or methods.
  6. If the prototype object is used to override the the previously set property it will not override the properties of already created objects. It will only affect the objects that have been created after the prototype change.

Using prototype on custom objects:

var func = function(){}; = "bar"; //adds the property foo
alert(; //alerts undefined
func2 = new func();
alert(; //alerts bar
func2.prototype.foo2 = 'ddd'; //Error : func2.prototype is undefined

Using prototype on builtin JS objects: = "bar"; = "bar"; = "bar"; = "bar";

You cannot use prototype on derived objects:

var str = new String(); = 'bar'; //Error : str.prototype is undefined


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s