On Aug 13, 11:50 am, jman <erjdri...@gmai l.comwrote:
i've got an object and i'd like to recursively call a function
within the class definition.
(i've simplified the code )
function myclass()
{
this.loop = function(index)
{
// ..work
setTimeout( "loop(" + (index+1) + ")", 100 );
}
}
does not work.
i've also tried
setTimeout( function() { loop(index+1) }, 100 );
i've also prefixed this. to loop in both cases - and no workee.
i think it's some sort of scope problem.
Indeed. What you're missing here is more basic than recursion - it's
a fundamental lack of understanding of the nature of JavaScript
scope. Unlike C++ or Java, the "this" keyword is not scoped, by
default, to the current object's context - it must always be
explicitly written out, and doesn't always refer to the object you
might think it does (from a classical OOP background). See
http://developer.mozilla.org/en/docs...:this_Operator
Another problem here is passing a string as an argument to
setTimeout. While it may technically work, it's terribly inefficient
- by using it, you're invoking the JS compiler to run your string as
code. Pass in a function instead, as you do in your second example.
-David