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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:39:11+00:00 2026-05-26T11:39:11+00:00

Normally I’d assign an alternative self reference when referring to this within setInterval. Is

  • 0

Normally I’d assign an alternative “self” reference when referring to “this” within setInterval. Is it possible to accomplish something similar within the context of a prototype method? The following code errors.

function Foo() {}
Foo.prototype = {
    bar: function () {
        this.baz();
    },
    baz: function () {
        this.draw();
        requestAnimFrame(this.baz);
    }
};
  • 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-26T11:39:12+00:00Added an answer on May 26, 2026 at 11:39 am

    Unlike in a language like Python, a Javascript method forgets it is a method after you extract it and pass it somewhere else. You can either

    Wrap the method call inside an anonymous function

    This way, accessing the baz property and calling it happen at the same time, which is necessary for the this to be set correctly inside the method call.

    You will need to save the this from the outer function in a helper variable, since the inner function will refer to a different this object.

    var that = this;
    setInterval(function(){
        return that.baz();
    }, 1000);
    

    Wrap the method call inside a fat arrow function

    In Javascript implementations that implement the arrow functions feature, it is possible to write the above solution in a more concise manner by using the fat arrow syntax:

    setInterval( () => this.baz(), 1000 );
    

    Fat arrow anonymous functions preserve the this from the surrounding function so there is no need to use the var that = this trick. To see if you can use this feature, consult a compatibility table like this one.

    Use a binding function

    A final alternative is to use a function such as Function.prototype.bind or an equivalent from your favorite Javascript library.

    setInterval( this.baz.bind(this), 1000 );
    
    //dojo toolkit example:
    setInterval( dojo.hitch(this, 'baz'), 100);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Normally when you iterate through a mysql result you do something like this: while
Normally this would be something like: glutAddMenuEntry(-, <opid>); But it doesn't work. After looking
Normally you can do this: <select size=3> <option>blah</option> <option>blah</option> <option>blah</option> </select> And it would
Normally to realized linked objects, i usually use getter and setter methods and this
Normally I would refrain from asking vague questions but in this case I cannot
Normally you write a handler for a button click like this: $(document).ready(function() { $(button).click(function()
normally JavaScript’s toString() method returns the array in a comma seperated value like this
Normally in a web app i would put a font-family in the CSS something
Normally, I'd choose List<String> [or, in VB, List(Of String) ] over StringCollection whenever possible:
Normally, we can create properties like this, dynamic expando = new ExpandoObject(); expando.Price =

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.