3

i know how to do inheritance in javascript but i can only inherit a single object. eg.

function fun1() {
this.var1=10;
this.meth1=function() {
...
...
};
}

function fun2() {
this.var2=20;
this.meth2=function() {
...
...
};
}

function fun3() {
this.var3=30;
this.meth3=function() {
...
...
};
}

now if i want an fun3 object to inherit fun1 object i can do this

fun3.prototype=new fun1();

or to inherit fun2 object i can do this

fun3.prototype=new fun2();

but how can i inherit both fun1 and fun2?

lovesh
  • 5,235
  • 9
  • 62
  • 93
  • 4
    The "like C++" part of your question is misguided. Single inheritance in Javascript already doesn't work like C++ in many ways... – Merlyn Morgan-Graham Jul 31 '11 at 06:07
  • 1
    @Merlyn Morgan-Graham i was just referring to a feature – lovesh Jul 31 '11 at 06:11
  • @Merlyn you can emulate classical inheritance in JavaScript so I'm guessing he hopes he can do the same thing with multiple inheritance. – Lime Jul 31 '11 at 06:11

4 Answers4

7

Technically, JavaScript does not offer multiple inheritance. Each object has a well-defined single "prototype" object, and thus a "prototype chain".

However, it is possible to augment any object with additional methods, so-called "expandos". So you could iterate over a collection of methods and individually add them to newly created objects. Such a collection is called "mixin".

Several frameworks offer mixins, for example:

  • qooxdoo
  • ExtJS
  • mootools
  • ...

They all work pretty much the same.

Note however that this is not real inheritance, since changes to the mixin will not be reflected in the objects.

For example:

var mixin = {
    method: function () {
        console.log('Hello world!');
    }
};
var foo = new fun1();
foo.method = mixin.method;
foo.method(); // Hello world!
mixin.method = function () { console.log('I changed!') };
foo.method(); // Hello world!
user123444555621
  • 148,182
  • 27
  • 114
  • 126
3

Javascript supports mixins which are (in my opinion) way better than C++ multiple inheritance. One has to change the thinking from the C++ way to appreciate how useful mixins can be. You can read about them:

Fresh Look at Mixins

Wikipedia Mixins

As many references as you want to read

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • thanks for the links,+1.you are right its taking me some time to think in javascript way. i was stuck on classical inheritance for a lot of time.But why do u think it is better than classical inheritance? – lovesh Jul 31 '11 at 06:36
  • The fact that you just add any behavior you want to any object by just adding properties and methods at any time to any object is just more flexible than the C++ way which has to be predetermined at compile time. IMO, the Javascript way is closer to what a programmer really wants. I have an object X and I want to give it behavior Y. In Javascript, I can just do that at any time with any object with any combination of behaviors at runtime. In C++, everything has to be predeclared before compile time. – jfriend00 Jul 31 '11 at 13:49
1

Here is an example of how to do that:

http://www.amirharel.com/2010/06/11/implementing-multiple-inheritance-in-javascript/

Victor
  • 11
  • 2
1

Well you could simply have fun2 inherit from fun1 to begin with.

fun2.prototype = new fn1;

If the above doesn't work for you, then unfortunately you can't do live multiple inheritance. You could copy the properties over to the the new Object, but then it isn't really 'live' inheritance.

For Example:

func3.prototype = new fun1();
for(var i in func2.prototype)func3.prototype[i]=fun2.prototype[i];
Lime
  • 13,400
  • 11
  • 56
  • 88