Opened 13 years ago
Closed 11 years ago
#5404 closed bug (fixed)
remove uses of 'var self = this;'
Reported by: | rdworth | Owned by: | |
---|---|---|---|
Priority: | minor | Milestone: | 1.9.0 |
Component: | [meta] ui.dev | Version: | 1.8 |
Keywords: | Cc: | ||
Blocked by: | Blocking: |
Description (last modified by )
DO NOT TRY TO FIX THIS UNTIL RIGHT BEFORE 1.9
Fixing this any sooner will cause merge conflicts for pull requests
var self = this;
is used in many plugins but not recommended best practice because if self isn't defined in a scope it will reference window.self
It should be removed from all plugins and replaced by something like 'that' or '_this'.
Change History (8)
comment:1 follow-up: 2 Changed 13 years ago by
comment:2 Changed 13 years ago by
Replying to AzaToth:
doesn't "var self=this;" define (and declare at the same time) self in the scope? Or is my understanding of javascript totally whacked? (or did you mean simple "self=this;" without the var part)
The problem is if you forget to include the assignment altogether. This isn't that uncommon if code is moved from one function to another. If the original function defines self and the new function doesn't, you could have a problem. In some cases, it's not obvious that the code is broken, because you may not get errors as an undefined self will reference window (try running window.self === window in a console). This could lead to storing values on the window instead of the plugin instance, and your plugin would continue to work properly, but would leak data into the global scope. Using a name other than self reduces the chances of this mistake going unnoticed.
comment:3 Changed 13 years ago by
And that's not just a theoretical problem. Cost me a few hours at the time.
comment:4 Changed 13 years ago by
Description: | modified (diff) |
---|
comment:5 Changed 12 years ago by
Status: | new → open |
---|
comment:6 Changed 12 years ago by
comment:7 Changed 12 years ago by
Description: | modified (diff) |
---|
comment:8 Changed 11 years ago by
Resolution: | → fixed |
---|---|
Status: | open → closed |
doesn't "var self=this;" define (and declare at the same time) self in the scope? Or is my understanding of javascript totally whacked? (or did you mean simple "self=this;" without the var part)