Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8187311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:31:10+00:00 2026-06-07T02:31:10+00:00

I am developing a browser based game on a node.js server. I’m trying to

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T02:31:11+00:00Added an answer on June 7, 2026 at 2:31 am

    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?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am developing a browser based game, and I have a big map there.
I am developing a browser based game and i'd like to know which functions
We are developing a live-stream, browser based racing game. Our contenders will start to
I am developing a client-server-based web application using a canvas component to display data
I'm developing a browser-based mobile app built on top of jQuery Mobile, and some
I'm developing a js application on a CE-HTML browser based television. So I have
I'm working on a browser based app, currently I'm developing and styling for the
I started developing a web-socket based game using the em-websocket gem. To test the
I am developing a SilverLight application wherein on browser close event, i need to
I am developing an Django application using the Django 1.3.1 release : https://code.djangoproject.com/browser/django/tags/releases/1.3.1 I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.