Book Image

Object-Oriented JavaScript

Book Image

Object-Oriented JavaScript

Overview of this book

Table of Contents (18 chapters)
Object-Oriented JavaScript
Credits
About the Author
About the Reviewers
Preface
Built-in Functions
Regular Expressions
Index

Multiple Inheritance


M ultiple inheritance is where a child inherits from more than one parent. Some OO languages support multiple inheritance, and some don't. You can argue both ways: that multiple inheritance is convenient, or that it's unnecessary, complicates application design, and it's better to use an inheritance chain instead. In any event, it's easy to implement multiple inheritance in dynamic languages such as JavaScript, although the language doesn't have a specific syntax for it. Leaving the discussion of multiple inheritance's pros and cons to the long, cold winter nights, let's see how you can do it in practice.

The implementation can be pretty simple. Just take the idea of inheritance by copying properties, and expand it so that it takes an unlimited number of input objects to inherit from.

Let's create a multi() function that accepts any number of input objects. You can wrap the loop that copies properties in another loop that goes through all the objects passed as arguments...