2

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?

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

4 Answers 4

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!
Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

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?
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.
1

Here is an example of how to do that:

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

Comments

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];

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.