In JavaScript, Prototype inheritance is much easier than you would have imagined. Simply understand the fundamentals, like we always do!

Inheritance in JavaScript is prototype-based. Every constructor has a property called prototype. For more information about the basics of prototypes, click here.

Master constructor object ‘Object’

Object is the master constructor in JavaScript. Don’t forget to take a note of the capitalization of letter ‘O’ in Object. It’s a

function constructor. Directly or indirectly, every object is created by using Object (master object). Since, it is available in the global space, you can access it directly. If you console.dir(Object) , you can view all methods that are available to Object . One of the methods is create() . It enables prototypal inheritance in JavaScript.

console.dir(Object);

Syntax

Object.create(prototypeObject, propertiesObject);

The Object.create method takes two arguments. First argument must be either an object or null. The second argument is optional and used to set properties. These properties are set in object’s prototype.

Creating Objects

const myObject = Object.create(Object.prototype); console.dir(myObject);

In the above example, the myObject object is created by using Object ’s prototype. This means myObject’s creator is Object.prototype . If you inspect myObject, you can view the following:



myObject does not contain anything. However, its __proto__ property is a reference to the Object ’s prototype. Hence, you can call methods on myObject that are available in Object ’s prototype. If you create an object literal by using const myLiteral = {}; , it possesses the same __proto__ because internally every object’s __proto__ is set to Object.prototype .To create an object without setting its __proto__ reference to Object.prototype, pass null as an argument. To verify this, inspect the object. As shown in the image below, it does not contain __proto__ property.

const myObject = Object.create(null); console.dir(myObject);

The Object.create() method also creates objects from constructors. It is similar to using the new keyword. The only difference is that by using the new keyword, you can run the constructor. Hence, it will set the initial properties in the constructor. Whereas, the Object.create() method doesn’t run the constructor and hence the initial properties are not set.

const Car = function(color) { this.color = color; }; let car1 = new Car('red'); let car2 = Object.create(Car.prototype); console.dir(car1); console.dir(car2);

Extending a Constructor

The main use of the Object.create() method is to extend the constructor. This means that you can inherit methods from a constructor’s prototype properties to other constructors. To extend a constructor, set a prototype reference of one object to another by using the Object.create() method.

Note: Only the constructor’s prototype is inherited and not the constructor.

const Car = function(color) { this.color = color; }; Car.prototype.getColor = function() { return this.color; } const ToyCar = function() {}; ToyCar.prototype = Object.create(Car.prototype); const legoCar = new ToyCar(); console.log(legoCar);







Verifying Prototype

In the above example, ToyCar’s __proto__ is set to Car and Car’s __proto__ is set to Object . The isPrototypeOf method verifies if legoCar, which is originally created by using the ToyCar constructor, is a prototype of ToyCar, Car and Object. The instanceof operator also performs the similar task.

const Car = function() {}; const ToyCar = function() {}; ToyCar.prototype = Object.create(Car.prototype); const legoCar = new ToyCar(); // Using the isPrototypeOf method console.log(ToyCar.prototype.isPrototypeOf(legoCar)); //true console.log(Car.prototype.isPrototypeOf(legoCar)); //true console.log(Object.prototype.isPrototypeOf(legoCar)); //true // Using the instanceof operator console.log(legoCar instanceof ToyCar); //true console.log(legoCar instanceof Car); //true console.log(legoCar instanceof Object); //true

Conclusion