Skip to main content

Search and Top Navigation

#5404 closed bug (fixed)

Opened March 22, 2010 08:32PM UTC

Closed May 10, 2012 12:21AM UTC

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

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

http://www.javascriptkata.com/2007/05/14/how-to-use-the-self-with-object-oriented-javascript-and-closures/#dsq-comment-43493993

It should be removed from all plugins and replaced by something like 'that' or '_this'.

Attachments (0)
Change History (8)

Changed April 30, 2010 11:31PM UTC by AzaToth comment:1

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)

Changed July 29, 2010 07:57AM UTC by scottgonzalez comment:2

Replying to [comment:1 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.

Changed July 29, 2010 09:10AM UTC by jzaefferer comment:3

And that's not just a theoretical problem. Cost me a few hours at the time.

Changed July 29, 2010 09:49AM UTC by rdworth comment:4

description: 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 \ \ http://www.javascriptkata.com/2007/05/14/how-to-use-the-self-with-object-oriented-javascript-and-closures/#comment-220 \ \ It should be removed from all plugins and replaced by something like 'that' or '_this'. \ \ 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 \ \ http://www.javascriptkata.com/2007/05/14/how-to-use-the-self-with-object-oriented-javascript-and-closures/#dsq-comment-43493993 \ \ It should be removed from all plugins and replaced by something like 'that' or '_this'. \ \

Changed March 28, 2011 02:21PM UTC by gnarf comment:5

status: newopen

Changed May 17, 2011 07:19PM UTC by davidmurdoch comment:6

Changed May 18, 2011 03:57PM UTC by scottgonzalez comment:7

description: 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 \ \ http://www.javascriptkata.com/2007/05/14/how-to-use-the-self-with-object-oriented-javascript-and-closures/#dsq-comment-43493993 \ \ It should be removed from all plugins and replaced by something like 'that' or '_this'. \ \ '''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 \ \ http://www.javascriptkata.com/2007/05/14/how-to-use-the-self-with-object-oriented-javascript-and-closures/#dsq-comment-43493993 \ \ It should be removed from all plugins and replaced by something like 'that' or '_this'. \ \

Changed May 10, 2012 12:21AM UTC by scottgonzalez comment:8

resolution: → fixed
status: openclosed