I saw the following in the source for WebKit HTML 5 SQL Storage Notes Demo:
function Note() {
var self = this;
var note = document.createElement('div');
note.className = 'note';
note.addEventListener('mousedown', function(e) { return self.onMouseDown(e) }, false);
note.addEventListener('click', function() { return self.onNoteClick() }, false);
this.note = note;
// ...
}
The author uses self in some places (the function body) and this in other places (the bodies of functions defined in the argument list of methods). What's going on? Now that I've noticed it once, will I start seeing it everywhere?
The variable is captured by the inline functions defined in the method.
this
in the function will refer to another object. This way, you can make the function hold a reference to thethis
in the outer scope.Yes, you'll see it everywhere. It's often
that = this;
.See how
self
is used inside functions called by events? Those would have their own context, soself
is used to hold thethis
that came intoNote()
.The reason
self
is still available to the functions, even though they can only execute after theNote()
function has finished executing, is that inner functions get the context of the outer function due to closure.See this article on alistapart.com
self
is being used to maintain a reference to the originalthis
even as the context is changing. It's a technique often used in event handlers (especially in closures).It should also be noted there is an alternative Proxy pattern for maintaining a reference to the original
this
in a callback if you dislike thevar self = this
idiom.As a function can be called with a given context by using
function.apply
orfunction.call
, you can write a wrapper that returns a function that calls your function withapply
orcall
using the given context. See jQuery'sproxy
function for an implementation of this pattern. Here is an example of using it:var wrappedFunc = $.proxy(this.myFunc, this);
wrappedFunc
can then be called and will have your version ofthis
as the context.As others have explained,
var self = this;
allows code in a closure to refer back to the parent scope.However, it's now 2018 and ES6 is widely supported by all major web browsers. The
var self = this;
idiom isn't quite as essential as it once was.It's now possible to avoid
var self = this;
through the use of arrow functions.In instances where we would have used
var self = this
:We can now use an arrow function without
var self = this
:Arrow functions do not have their own
this
and simply assume the enclosing scope.Actually self is a reference to window (
window.self
) therefore when you sayvar self = 'something'
you override a window reference to itself - because self exist in window object.This is why most developers prefer
var that = this
overvar self = this;
Anyway;
var that = this;
is not in line with the good practice ... presuming that your code will be revised / modified later by other developers you should use the most common programming standards in respect with developer communityTherefore you should use something like var
oldThis
/var oThis
/ etc - to be clear in your scope // ..is not that much but will save few seconds and few brain cycles