Google News
logo
Full Stack Developer - Interview Questions
Explain Prototype Inheritance in Javascript?
In a language implementing classical inheritance like Java, C#, or C++ you start by creating a class–a blueprint for your objects – and then you can create new objects from that class or you can extend the class, defining a new class that augments the original class.
 
In JavaScript you first create an object (there is no concept of class), then you can augment your own object or create new objects from it.
 
Every object in Javascript has a prototype. JavaScript’s inheritance system is based on prototypes, and not class-based. When a message reaches an object, JavaScript will make an attempt to find a property in that object first. If it cannot find it, then the message will be sent directly to the object’s prototype, and so on. That behavior called prototype chain or in many places, prototype inheritance.
 
Constructor functions are the most preferred way in JavaScript when it comes to constructing prototype chains. When we use new, JavaScript injects an implicit reference to the new object being created in the form of this keyword. It also returns this reference completely at the end of the function.
function Foo() {
  this.kind = ‘foo’
}

var foo = new Foo(); 
foo.kind //=> ‘foo’
Advertisement