Impact

This forum is read only and just serves as an archive. If you have any questions, please post them on github.com/phoboslab/impact

1 decade ago by Matthias

Hey everyone,
I'm wondering if it's possible to inherit from multiple classes when defining a new entity. This didn't work:

EntityDoor = EntityHasHandle.extend( {

...

});

EntityDoor = EntityPushable.extend({

...
});

I know that it's possible to have chained inheritance, that is, an object A can inherit from an object B that inherits from object C, and so on. But here I have a situation where the classes B and C that I want to inherit from do not stand in an inheritance relation by themselves.

I've already read the class documentation at
http://impactjs.com/documentation/class-reference/class
The only thing I thought about was that it may be possible to use a combination of inheritance and injection. I.e. one would inherit from class B and then inject the contents of class C. But that doesn't seem possible, as inject() seems to accept only text and no class objects.

Any ideas? In the worst-case I'd just have to create a mega class, i.e., let B inherit from C - that wouldn't be very elegant though because it doesn't make much sense but I guess it would work.

1 decade ago by dominic

Yep:
var PushableWithHandle = EntityPushable.extend(EntityHasHandle.prototype);
EntityDoor = PushableWithHandle.extend({ ... });

Or in short:
EntityDoor = EntityPushable.extend( EntityHasHandle.prototype ).extend({ 
    ... 
});

1 decade ago by tarrent

Quote from dominic
Yep:
var PushableWithHandle = EntityPushable.extend(EntityHasHandle.prototype);
EntityDoor = PushableWithHandle.extend({ ... });

Or in short:
EntityDoor = EntityPushable.extend( EntityHasHandle.prototype ).extend({ 
    ... 
});


Thanks for this useful info I was about to ask this too.

1 decade ago by Matthias

Thanks for the info Dominic, apreciate it! Just another quick question: what is the instanceof() operator's definition for multi-inheritance. If we had

EntityA = EntityB.extend( EntityC.prototype).extend({ });

and an instance of EntityA, a. Would instanceof(a) result in true for both EntityB and EntityC?
Page 1 of 1
« first « previous next › last »