3

The MDN gives this explanation of inheritance in Javascipt (with the comments showing the prototype chain):

var a = {a: 1}; 
// a ---> Object.prototype ---> null

var b = Object.create(a);
// b ---> a ---> Object.prototype ---> null
console.log(b.a); // 1 (inherited)

var c = Object.create(b);
// c ---> b ---> a ---> Object.prototype ---> null

var d = Object.create(null);
// d ---> null
console.log(d.hasOwnProperty); 
// undefined, because d doesn't inherit from Object.prototype

Here it looks to me like c is inheriting from multiple classes. How is this not multiple inheritance?

2 Answers 2

8

Multiple inheritance is when the parents are on the same level in the hierarchy:

c ---> b ---> Object.prototype ---> null
 \---> a ---> Object.prototype ---> null

In this case, it's simple inheritance from a class b which inherits from an other class a:

c ---> b ---> a ---> Object.prototype ---> null

Addendum: While the effects might seem similar (attributes of b will be also "found" in c via lookup in the prototype chain), do note the difference that multiple inheritance would allow a and b to have entirely different inheritance chains (in fact, inheritance "trees"), which is clearly not the case in your example.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the edits @zerkms, I was just about to add something like that myself.
I hope my ASCII art isn't too ugly though :-)
I see. So just to be completely clear, in the question's example, the object is inheriting from other objects higher in the chain, not on the same level?
@Startec - I'm not sure I follow - that chain is THE inheritance chain, there's no inheriting from outside it by definition, and there's no elements on the same level, because it's a one-by-one chain.
1

Multiple inheritance means inheriting in parallel from two or more classes, i.e. having two or more direct ancestors. They form a tree.

In your case there's only one direct ancestor: b, and a is b's direct ancestor, but indirect of c. They form a linear chain.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.