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 7913523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:51:49+00:00 2026-06-03T13:51:49+00:00

My situation is that I am working on a Canvas game using Box2DWeb, so

  • 0

My situation is that I am working on a Canvas game using Box2DWeb, so I have a class called Sprite that draws an image to the Canvas, I now want to create a PhysicsSprite class that inherits methods and properties from Sprite.

My Sprite class (Sprite.js):

Sprite = function (position, dimension, image) {

  this.Position = position;
  this.Dimension = dimension;
  if (image)
    this.Image = image;
  ...

  this.Draw = function (g) {
    ...
  }
  ...
};

And I am setting the prototype in PhysicsSprite (PhysicsSprite.js) like so:

PhysicsSprite = function(position, dimension, world) {

    this.prototype = new Sprite(position, dimension, undefined);
    //alert(this.prototype.Position.X)

    var body = CreateBody();
    this.GetBody = function () { return body; }

    this.Draw = function (g) {
        ...  
    }

    function CreateBody () {
          ...
    }
};

Note the alert(this.prototype.Position.X), which does alert me of the position, but if I change the code to this.Position.X, I get an error, and similarly, if I have an instance of PhysicsSprite, I have to explicitly call the prototype.

This lead me to think that I haven’t set the Object’s prototype, merely created a property called prototype, and set that to a new Sprite.

If someone could help me, and explain what I am doing wrong, that would be much appreciated.
I am dyslexic, so I always misspell variables, which is frustrating, so that was the first thing I looked for, but everything looks fine to me.

  • 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-03T13:51:52+00:00Added an answer on June 3, 2026 at 1:51 pm

    This lead me to think that I haven’t set the Object’s prototype, merely created a property called prototype, and set that to a new Sprite.

    That’s right. Also, you are not making use of the function’s prototype at all, since you assign each function directly to the instance (this.Draw = function() ...) instead of the prototype.


    In JavaScript you have something called constructor functions. Every function called with the new operator is a constructor function.

    When a function is called that way (new SomeFunc()), a new object is created (lets call it an isntance) which inherits from the object referenced by SomeFunc.prototype and this instance is available inside that function via this.

    Now, inheritance basically comes down to having a prototype (SomeFunc.prototype) which instead of being an (nearly) empty object (the default), inherits from another function’s prototype.

    Example:

    function A(name) {
        // `this` refers to a new instance
        // lets set some properties:
        this.name = name;
    }
    
    // all "class" methods should be assigned to the prototype
    A.prototype.getName = function() {
        return this.name;
    };
    
    
    // lets create a child "class"
    function B(name, place) {
        // we have to call the parents constructor with the current instance
        // and the arguments we want to pass on.
        // this is like `super(name)` in Java or `A.__init__(self, name)` in Python 
        // (or `super(B, self).__init__(name)` in Python)
        A.call(this, name);
        this.place = place;
    }
    
    // here we connect A's prototype with B's prototype in a way that they
    // stay independent 
    inherits(B, A);
    
    // B's "class" methods
    B.prototype.getPlace = function() {
        return this.place;
    };
    
    // now we can do
    var b = new B('SomeName', 'SomePlace');
    alert(b.getName());
    alert(b.getPlace());
    

    This is how inherits could look like (this implementation is used by the Google Closure library):

    function inherits(Child, Parent) {
        var Tmp = function() {};
        Tmp.prototype = Parent.prototype;
        Child.prototype = new Tmp();
        Child.prototype.constructor = Child;
    }
    

    You see that Child.prototype refers to an instance of Tmp. But Tmp‘s prototype is the same as Parent‘s prototype. That means, the instance returned by new Tmp() inherits from Parent‘s prototype, and since this instance is the prototype of Child, all instances of Child will inherit from Parent.prototype as well.

    With ECMAScript 5, this can be simplified to

    Child.prototype = Object.create(Parent.prototype);
    Child.prototype.constructor = Child;
    

    but it’s essentially the same. It creates a new object which inherits from Parent.prototype.


    Further reading:

    • JS: Confusion about inheritance
    • MDN – Inheritance and the prototype chain
    • MDN – Inheritance revisited
    • MDN – new
    • MDN – this
    • MDN – .call()
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been working on optimizing a query and have ran into a situation that's
We have situation where say we have four engineers that are working on software
Here is the situation: the company that I'm working in right now gave me
I have this situation that is really irritating me now. At random times with
I have a somewhat unique situation that I can't quite get working. I've followed
I recently encountered a situation in some code I am working on that doesn't
Situation: We are working on a project that reads datafeeds into the database at
Here's the situation: I'm currently working on a substantially large .Net application that makes
I have a situation that has been partially covered by other answers at SO,
I have a situation that requires redirecting users who are already logged in away

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.