Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use self in JavaScript

Tags:

javascript

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?

like image 236
Konrad Avatar asked Jul 22 '10 13:07

Konrad


People also ask

Why Self is used in JavaScript?

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.

What is self JavaScript?

"self" has no special syntactic meaning, it is just an identifier. Browsers tend to define window. self (just a property of the global window object) = window.

What is top and self in JavaScript?

In the Window object, top references the topmost window. While self references the current window.

Is JavaScript This same as Python self?

01:16 This highlights how this is different than self in Python. self is always within the context of an object. In JavaScript, the context is based on the calling function. If there is no calling function, then this means the global context.


2 Answers

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.

like image 97
Nick Craver Avatar answered Oct 16 '22 10:10

Nick Craver


It works with setTimeout because of two conditions in the browser:

  • All global variables are properties of the window object. That means that window has a property setTimeout (window.setTimeout).
  • The window object has a property called self that points to itself.

As you can access the properties of window without explicitly writing window (that is what makes the global variables global), both calls work: setTimeout() will look up the property setTimeout() on the window object. self.setTimeout() will look up the property self on the window object, which is the window object itself.

So if you call self.setTimeout() it is the same as window.self.setTimeout() which is the same as window.setTimeout() which again is the same as setTimeout().

Note: This only works if there is no variable self defined in the current scope that shadows the global self.

This works with any symbol (meaning variable or function) defined in the global scope. You can test it yourself:

alert(window.self); 

and

alert(self); 

should both alert

[object Window] 
like image 26
Felix Kling Avatar answered Oct 16 '22 11:10

Felix Kling