0

I heard that multiple inheritance is not supported in JavaScript.

Does the below example can be considered as Multiple Inheritance if no then can anyone please explain why...?

function A () {
  this.a = 'a'
};

function B () {
  this.b = 'b'
};

function AB () {
  A.call(this);
  B.call(this);
};

var obj = new AB();
console.dir(obj);
//Output: {a: "a", b: "b"}
Mr. A
  • 77
  • 2
  • 8
  • 1/2 :: For following reasons I neither do consider the question nor the 2 answers as "primarily opinion based". Firstly, the OP's question is straightforward towards one goal, accompanied by an example that is excellently chosen for it directly points to the way of looking at this very problem, especially in JS with its functions as constructors, prototypes linked to constructors and with context that can be delegated to functions via call and apply. (opinion: thus the OP's question has not deserved being down-voted as it has happened.) ... – Peter Seliger Jan 04 '17 at 13:35
  • ... 2/2 :: Secondly, @zzzzBov varies the given example 3 times in order to show the difference between function based object composition and constructor and prototype based inheritance in JS. Thirdly, both answers state that composition can bee seen as alternative to multiple inheritance which also is not an unjustified opinion. – Peter Seliger Jan 04 '17 at 13:47

2 Answers2

3

Can the below example be considered Multiple Inheritance?

No

Can anyone please explain why?

Your AB (which I will call C from here on out) function doesn't actually extend A nor does it extend B:

function A () {
  this.a = 'a';
}

function B () {
  this.b = 'b';
}

function C () {
  A.call(this);
  B.call(this);
}

a = new A();
console.log('a is A', a instanceof A);

b = new B();
console.log('b is B', b instanceof B);

c = new C();
console.log('c is A', c instanceof A, 'c is B', c instanceof B);

You don't have any inheritance at all in that example. You do have function composition.

If you wanted to have inheritance, the C function would need to have a prototype that points to an instance of either A or B:

function A () {
  this.a = 'a';
}

function B () {
  this.b = 'b';
}

function C () {
  A.call(this);
  B.call(this);
}

C.prototype = new A();
//or
//C.prototype = new B();

a = new A();
console.log('a is A', a instanceof A);

b = new B();
console.log('b is B', b instanceof B);

c = new C();
console.log('c is A', c instanceof A, 'c is B', c instanceof B);

Note that because the C function has a single prototype you can only have single inheritance.


For object composition, it would be common to see a pattern along the lines of:

function A () {
  this.a = 'a';
}

function B () {
  this.b = 'b';
}

function C () {
  this.a = new A();
  this.b = new B();
}

a = new A();
console.log('a is A', a instanceof A);

b = new B();
console.log('b is B', b instanceof B);

c = new C();
console.log('c.a is A', c.a instanceof A, 'c.b is B', c.b instanceof B);
zzzzBov
  • 174,988
  • 54
  • 320
  • 367
2

What you have done is called composition and it is often an alternative to multiple inheritance.

Here are some references that might help you

Community
  • 1
  • 1
Jeremy J Starcher
  • 23,369
  • 6
  • 54
  • 74