Search⌘ K
AI Features

Solution Review: Prototypal Inheritance

Understand how to implement prototypal inheritance in JavaScript by calling parent constructors, creating prototype chains, and managing the constructor property. This lesson clarifies inheritance concepts that are essential for JavaScript object-oriented programming.

We'll cover the following...

Solution #

Javascript (babel-node)
function Human(name, age) {
this.name = name;
this.age = age;
};
function Man(name,age) {
Human.call(this, name, age);
};
Man.prototype = Object.create(Human.prototype);
Man.prototype.constructor = Man;
function check(){
var obj = new Man("Tommy Tan",20);
console.log(obj.name)
console.log(obj instanceof Human)
}
check()

Explanation #

In the code above, Human is the parent from which the child, Man, inherits. For it to inherit the ...