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

  • Home
  • SEARCH
  • 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 6547155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:47:44+00:00 2026-05-25T11:47:44+00:00

I think I’m missing some key concept regarding objects and prototype functions in JavaScript.

  • 0

I think I’m missing some key concept regarding objects and prototype functions in JavaScript.

I have the following:

function Bouncer(ctx, numBalls) {
    this.ctx = ctx;
    this.numBalls = numBalls;
    this.balls = undefined;
}

Bouncer.prototype.init = function() {
    var randBalls = [];
    for(var i = 0; i < this.numBalls; i++) {
        var x = Math.floor(Math.random()*400+1);
        var y = Math.floor(Math.random()*400+1);
        var r = Math.floor(Math.random()*10+5);
        randBalls.push(new Ball(x, y, 15, "#FF0000"));
    }
    this.balls = randBalls;
    this.step();
}

Bouncer.prototype.render = function() { 
    this.ctx.clearRect(0, 0, 400, 400);  
    for(var i = 0; i < this.balls.length; i++) {
        this.balls[i].render(this.ctx);
    }
}

Bouncer.prototype.step = function() {
    for(var i = 0; i < this.balls.length; i++) {
        this.balls[i].yPos -= 1;
    }      
    this.render();
    setTimeout(this.step, 1000);
}

I then create an instance of Bouncer and call its init function like so:

$(function() {
    var ctx = $('#canvas')[0].getContext('2d');
    var width = $('#canvas').width();
    var height = $('#canvas').height();



    var bouncer = new Bouncer(ctx, 30);
    bouncer.init();
});

The init() function will call step which has a setTimeout to loop the step function.

This works on the first call to step(). However, on the second call (when setTimeout fires step) the instance variable “balls” is undefined. So, in my step function, the second call will blow up saying there is no “length” property for undefined.

Why do I lose my instance information when calling step from setTimeout()?

How could I restructure this so I can loop via a timeout and still have access to those instance variables?

  • 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-05-25T11:47:45+00:00Added an answer on May 25, 2026 at 11:47 am

    When you call setTimeout(this.step, 1000);, the step method loses its desired context of this, as you’re passing a reference to the step method. In the way that you’re doing it now, when this.step gets called through setTimeout, this === window rather than your Bouncer instance.

    This is easy to fix; just use an anonymous function, and keep a reference to this:

    Bouncer.prototype.step = function() {
        var that = this; // keep a reference
        for(var i = 0; i < this.balls.length; i++) {
            this.balls[i].yPos -= 1;
        }      
        this.render();
        setTimeout(function () { 
            that.step()
        }, 1000);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

think about having a skript where you have some words as input. now you
Think I'm missing a basic concept. I want to generate html by traversing through
I think I may have some confusion about the domain/service layer separation. In my
Think about the following: Your ISP offers you a dynamic ip-address (for example 123.123.123.123).
I think this is specific to IE 6.0 but... In JavaScript I add a
Think of the following code: static int Main() { byte[] data = File.ReadAllBytes(anyfile); SomeMethod(data);
Think Design: I have many applications that share the same user database! Other tables
Think of the following services on one box: SOCKS proxy HTTP proxy SSH service
Think 2 entities OneToOne mapped. Person and Car. A Person can have a Car.
I think that I am having a slow evening but I have a question

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.