O'Reilly logo

Learning Object-Oriented Programming by Gastón C. Hillar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required