4

Within my a certain function of a class, I need to use setInterval to break up the execution of the code. However, within the setInterval function, "this" no longer refers to the class "myObject." How can I access the variable "name" from within the setInterval function?

function myObject() {
    this.name = "the name";
}

myObject.prototype.getName = function() {
    return this.name;
}

myObject.prototype.test = function() {
    // this works
    alert(this.name);

    var intervalId = setInterval(function() {
        // this does not work
        alert(this.name);

        clearInterval(intervalId);
    },0);
}
1
  • Use of setInterval (esp. with a period of 0) is a bit odd. You only have it fire once anyway, why not use setTimeout which would make it simpler. Commented Jan 10, 2009 at 0:16

4 Answers 4

12
myObject.prototype.test = function() {
    // this works
    alert(this.name);
    var oThis = this;
    var intervalId = setInterval(function() {
        // this does not work
        alert(oThis.name);

        clearInterval(intervalId);
    },0);
}

This should work. The anonymous function's "this" is not the same "this" as your myObject's "this."

Sign up to request clarification or add additional context in comments.

5 Comments

I tend toward 'self' but that's me
I actually prefer to use "superThis" but it didn't seem appropriate for an example :)
"self" is a reserved keyword in javascript, you should'nt use it.
"self" is not a reserved word. It's just a special variable.
I also use self, its a very recognisable idiom. It is true that browsers also place a self property on the window object, I can't think why, if I want to access the window I use window.
1

Here's the prototype bind function

Function.prototype.bind = function( obj ) {
    var _this = this;
    return function() {
        return _this.apply( obj, arguments );
    }
}

1 Comment

This implementation of Function.bind() does not support partial function application as proposed for ECMAScript Harmony, so may cause compatibility problems further down the line. You can still use it of course, just best call it something else if you want to bang it into the Function prototype.
0

This is what binding is for in Prototype:

function myObject() {
    this.name = "the name";
}

myObject.prototype.getName = function() {
    return this.name;
}

myObject.prototype.test = function() {
    // this works
    alert(this.name);

    var intervalId = setInterval(function() {
        // this does not work
        alert(this.name);

        clearInterval(intervalId);
    }.bind(this),0);
}

2 Comments

I'm not using the Prototype framework, but this would work otherwise.
My apologies! Seeing the prototype keyword throws me off sometimes.
0

Please note that s13james's answer is incomplete in that bind() is not a standard feature and must be provided elsewhere - one way is using the prototype.js Javascript framework, while another will be to do it yourself using meouw's code example.

If you don't use bind() (which is nifty, I must say) then djangel's response is what you could have done, and is what I most often do.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.