O'Reilly logo

Mastering JavaScript Design Patterns - Second Edition by Simon Timms

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

Inheritance

One of the niceties of objects is that they can be built upon to create increasingly complex objects. This is a common pattern, which is used for any number of things. There is no inheritance in JavaScript because of its prototypical nature. However, you can combine functions from one prototype into another.

Let's say that we have a base class called Castle and we want to customize it into a more specific class called Winterfell. We can do so by first copying all of the properties from the Castle prototype onto the Winterfell prototype. This can be done like so:

let Castle = function(){}; Castle.prototype.build = function(){console.log("Castle built");} let Winterfell = function(){}; Winterfell.prototype.build = Castle.prototype.build; ...

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