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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
selfcan refer to the window object, but typically that’s not the case here. You’ll see this commonly above thatsetTimeout():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
thisdirectly, it would (most of the time) refer towindowand not the current object, whereas passing another variable in maintains the current reference.