Prototype in JavascriptJavascript 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. 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 chainsEvery 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. |
Got a thought to share or found a
bug in the code?
We'd love to hear from you:
Name: | |
Email: | (Your email is not shared with anybody) |
Comment: |
Facebook comments: