Make delicious recipes!

Prototype in Javascript

Javascript does not have classes and inheritance in the conventional sense as present in C++ or Java. Classes functionality is achieved in JS by prototype.

Anything added to a prototype is added to all the objects of that class.

So, using this, we can add properties/methods at runtime to a class. Example:

Inheritance in JavaScript

Inheritance can be achieved in JS by assigning the prototype of derived class to that of base class.
This can be done as:

var derived = function () {};

derived.prototype = new base ();

With the above, all objects made by calling ‘new derived()’ will have all the properties and methods of the base class.

Prototype chains

Every prototype also has a prototype and that again has a prototype.

So, following is true for an object:

a.prototype = b;

b.prototype = c;

c.prototype = d;

and so on till we reach Object.prototype whose prototype is null.

Like us on Facebook to remain in touch
with the latest in technology and tutorials!

Got a thought to share or found a
bug in the code?
We'd love to hear from you:

Email: (Your email is not shared with anybody)

Facebook comments:

Site Owner: Sachin Goyal