Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is multiple inheritance possible in javascript? [duplicate]

Tags:

javascript

I have a situation here. I have two modules(nothing but javascript function) defined like this:

Module1:

define(function(){
    function A() {
        var that = this;
        that.data = 1
        // ..
    }
    return A; 
});

Module2:

define(function(){   
    function B() {
        var that = this;
        that.data = 1;
        // ...
    }
    return B; 
});

How to inhert both modules inside other module?

like image 408
Rahul Pal Avatar asked Jan 22 '26 06:01

Rahul Pal


1 Answers

1) In js everything is just an object.

2) Javascript inheritance uses prototype inheritance and not classic inheritance.

JavaScript doesn't support multiple inheritance. To have both of them inside the same class try to use mixins that are better anyhow:

function extend(destination, source) {
  for (var k in source) {
    if (source.hasOwnProperty(k)) {
      destination[k] = source[k];
    }
 }
 return destination; 
 }

 var C = Object.create(null);
 extend(C.prototype,A);
 extend(C.prototype,B);

mixins:

http://javascriptweblog.wordpress.com/2011/05/31/a-fresh-look-at-javascript-mixins/

inheritance in js:

http://howtonode.org/prototypical-inheritance

http://killdream.github.io/blog/2011/10/understanding-javascript-oop/index.html

like image 131
Dory Zidon Avatar answered Jan 24 '26 18:01

Dory Zidon