1

I am developing a browser based game on a node.js server. I'm trying to implement a component based inheritance structure but I am unsure of the concepts with regards to javascript inheritance.

As far as I can make out, if I want to inherit from two component objects e.g. function Moves(){} and function IsVisible(){}, the usual prototypal inheritance doesn't work:

gameObject.prototype = new Moves;
gameObject.prototype = new IsVisible;

The is IsVisible object overwrites the Moves object as the gameObject prototype.

Below is my attempt at creating an inherit function that takes a target object and an arbitrary number of component objects as arguments. The target object then inherits the behavior and variables from the these components.

I'm not familiar with javascript inheritance and would like to know if the below method a good way of implementing a multiple inheritance type structure in javascript?

Are there any other well established practices dealing with this problem? (If it is a problem...)

Any help would be much appreciated.

function inherit(o){

    var proto = o;
    for(var i = 1; i < arguments.length; i++){
        var component =  new arguments[i]();
        for(var x in component){
            proto[x] = component[x];
        }
    }
}

function Player(name){

    var o = {};
    o.name = name;
    inherit(o, WorldSpace, Active);
    return o;

}

function WorldSpace(){
    this.pos = {x:0, y:0};
    this.orientation=0;
}

function Active(){
    this.acceleration = 1;
    this.velocity = {x:0,y:0};
    this.rotation = 0;
    this.rotationSpeed = (Math.PI*2)/30;
    this.throttle = false;
    this.currentSpeed = 0;
    this.maxSpeed = 10;
}
RobotEyes
  • 4,929
  • 6
  • 42
  • 57
  • Try making Player a constructor, and use the traditional way of javascript inheritance as can be found all over the web. – Bergi Jul 05 '12 at 00:28
  • @Bergi Javascript doesn't support multiple inheritance so using `person.prototype = new WorldSpace; person.prototype = new Active` wouldn't work. – RobotEyes Jul 05 '12 at 01:40
  • 1
    You don't want multiple inheritance, you want only mixins - you have no prototypes in your code. AaditMShah is perfectly right. – Bergi Jul 05 '12 at 09:35
  • 1
    @Bergi Your line is now my googlism: http://www.googlism.com/who_is/a/aaditmshah/ – Aadit M Shah Nov 17 '14 at 09:35

1 Answers1

4

From what I see in your code you don't really need multiple inheritance. You are simply trying to mix in code from different constructors into one. This is possible and is actually really simple to achieve in JavaScript.

First let's rewrite your code for Player:

function Player(name) {
    this.name = name;
    WorldSpace.apply(this);
    Active.apply(this);
}

That's it. Simple right? Let me explain how the apply function works:

  1. Every function in JavaScript has a special pointer called this.
  2. When you call a function normally this usually points to the global object (e.g. window).
  3. When you call a function preceded by the keyword new JavaScript creates an instance of that function and this points to that instance.
  4. JavaScript also has two functions call and apply which are methods of every function and allow you to call that function with any value for the this pointer.

So essentially when I call WorldSpace.apply with this as the first argument, the this pointer in the WorldSpace function points the this in Player. You are essentially using the WorldSpace and Active constructors as mixins.

If you want to know more about how inheritance works in JavaScript I suggest you read this answer. Have you considered using a framework to make your life simpler?

Community
  • 1
  • 1
Aadit M Shah
  • 72,912
  • 30
  • 168
  • 299
  • Brilliant, I knew there must be a better way of doing it. I'm not considering a framework at the moment to concentrate on learning core javascript. Thanks for your help. – RobotEyes Jul 05 '12 at 13:38