Possible Duplicate:
Pass correct “this” context to setTimeout callback?
I have a problem with my little script.
function test() {
this.arr = new Array('a','b','c');
this.func = function() {
//Do something with this.arr. But it's undefined.. WHY???
}
this.Start = function() {
this.Interval = setInterval(this.func, 1000);
}
}
var Test = new test();
Test.Start();
When I try to do anything with array in "func", it keeps telling me, that the array is undefined. Why?
You're getting the wrong this
reference, try:
function Test() {
this.arr = new Array('a','b','c');
this.func = function() {
console.log(this.arr);
};
this.start = function() {
var fun = this.func.bind(this);
this.interval = setInterval(fun, 1000);
};
}
var test = new Test();
test.start();
This article about this
is a really interesting if you need more information: Function invocation and this
Also, note that I've changed the case on some symbols. Remember, functions that are meant to be used as constructors start with a uppercase letter, variables and methods use lowercase.
Timers/Intervals are called on global scope so 'this' is set to 'window'. To get around this, you can pass a third option to setInterval() which will be used as a parameter for the specified function call.
function Test(){
this.array = new Array('a','b','c');
this.function = funct(){ }
this.start = function(){
this.Interval = setInterval(function(passedObj){
passedObj.funct.call(passedObj);
},1000,this);
}
}
var testObj = new Test();
testObj.start();
Reading the comments below, you will see this is meant for nodejs. This, however, will not work for IE8 and/or earlier.