Borrowing a constructor

One more way of implementing inheritance (the last one in the chapter, I promise) has to do again with constructor functions and not the objects directly. In this pattern, the constructor of the child calls the constructor of the parent using either the call() or apply() method. This can be called stealing a constructor or inheritance by borrowing a constructor if you want to be more subtle about it.

The call() and apply() methods were discussed in Chapter 4, Objects, but here's a refresher; they allow you to call a function and pass an object that the function should bind to its this value. So for inheritance purposes, the child constructor calls the parent's constructor and binds the child's newly created this object as ...

Get Object-Oriented JavaScript - Third Edition 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.