Inheritance

One of the niceties of objects is that they can be built upon to create increasingly more 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 as shown in the following code:

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

Get Mastering JavaScript Design Patterns 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.