Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
146
rated 0 times [  152] [ 6]  / answers: 1 / hits: 52303  / 14 Years ago, thu, july 22, 2010, 12:00:00

I've noticed that calls like setTimeout() work either as :



self.keyword()



or just on their own e.g. keyword().



What is the different between the two calls?


More From » javascript

 Answers
23

self can refer to the window object, but typically that's not the case here. You'll see this commonly above that setTimeout():



var self = this;


They're keeping a reference to the current object, so later when you call self.keyword() you're calling that method on that object, not any other.



Say you have for example images in the page you wanted to rotate every 2 seconds...you'd want each of those 3 timers to refer to their own methods. If they use this directly, it would (most of the time) refer to window and not the current object, whereas passing another variable in maintains the current reference.


[#96146] Tuesday, July 20, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
emiliano

Total Points: 381
Total Questions: 109
Total Answers: 93

Location: Jersey
Member since Fri, Oct 1, 2021
3 Years ago
emiliano questions
;