Created
July 6, 2015 17:41
-
-
Save Comandeer/52b82c5e74e2ce5c73c7 to your computer and use it in GitHub Desktop.
Multi inheritance in JS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var A = function() | |
{ | |
} | |
,B = function() | |
{ | |
} | |
,C = function() | |
{ | |
} | |
,createProto = function() | |
{ | |
var protos = [].slice.call(arguments) | |
,prototype = {}; | |
protos.forEach(function(proto) | |
{ | |
Object.keys(proto).forEach(function(key) | |
{ | |
prototype[key] = proto[key]; | |
}); | |
}); | |
return prototype; | |
}; | |
A.prototype = { | |
constructor: A | |
,metoda1: function() {} | |
,metoda2: function() {} | |
}; | |
B.prototype = { | |
constructor: B | |
,metoda3: function() {} | |
,metoda4: function() {} | |
}; | |
C.prototype = createProto(A.prototype, B.prototype); | |
var c = new C(); | |
console.log(c instanceof A, c instanceof B, c instanceof C); | |
C.prototype = new B(); | |
c = new C(); | |
console.log(c instanceof B, c instanceof C); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment