Reading Nodejs source code - How fn.call and util.inherits are used to extend Prototypal Chain?
Code
var util = require('util');
function Person() {
this.firstName = "John";
this.lastName = "Doe";
Vehicle.call(this);
}
Person.prototype.getFullName = function() {
return this.firstName + " " + this.lastName;
}
function Vehicle() {
this.car = "Honda";
this.wheels = 4;
this.getWheels = function() {
return this.wheels;
}
}
Vehicle.prototype.getCar = function() {
return this.car;
}
util.inherits(Person, Vehicle);
var michael = new Person();
console.log(michael);
// // Prototypal Chain for 'michael'
console.log("\nPrototypal Chain for 'michael'");
console.log(michael.__proto__);
console.log(michael.__proto__.__proto__);
console.log(michael.__proto__.__proto__.__proto__);
Output
Person {
firstName: 'John',
lastName: 'Doe',
car: 'Honda',
wheels: 4,
getWheels: [Function] }
Prototypal Chain for 'michael'
Person { getFullName: [Function] }
Vehicle { getCar: [Function] }
{}
Summary
-
Extends the prototypal chain
-
Gives access to methods and properties defined inside the function constructor and defined on the prototype property of the function constructor