Working with the prototype-based inheritance in JavaScript

First, we will create a constructor function in JavaScript and define properties and methods in its prototype. Then, we will take advantage of prototype-based inheritance in order to create objects that specialize the behavior defined in the baseline prototype. We will override methods and properties.

Creating objects that specialize behavior in JavaScript

Now it is time to code objects in JavaScript. The following code defines the Animal empty constructor function in JavaScript, followed by the declaration of properties and functions for the Animal prototype:

function Animal() {} Animal.prototype.numberOfLegs = 0; Animal.prototype.pairsOfEyes = 0; Animal.prototype.age = 0; Animal.prototype.printLegsAndEyes ...

Get Learning Object-Oriented Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.