[wp-trac] [WordPress Trac] #25970: jshint shouldn't throw errors: wp-includes/js/admin-bar.js

WordPress Trac noreply at wordpress.org
Thu Nov 14 01:16:55 UTC 2013


#25970: jshint shouldn't throw errors: wp-includes/js/admin-bar.js
--------------------------+-----------------------------
 Reporter:  kadamwhite    |      Owner:
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Build Tools   |    Version:
 Severity:  normal        |   Keywords:  has-patch
--------------------------+-----------------------------
 Aside from onevar fixes, this file has two JSHint directive changes: the
 inline'd jQuery hoverIntent plugin is ignored, and I have disabled
 `loopfunc` for this file.

 The purpose of `"loopfunc": true` is to prevent dangerous situations where
 defining a function within a loop yields unexpected results. From the
 JSHint docs, this is the antipattern example:

 {{{#!javascript
 var nums = [];

 for (var i = 0; i < 10; i++) {
   nums[i] = function (j) {
     return i + j;
   };
 }

 nums[0](2); // Prints 12 instead of 2
 }}}

 The issue is that `i` needs to be copied into an immediately-invoked
 function in order for the variable reference to be correctly interpreted:

 {{{#!javascript
 var nums = [];

 for (var i = 0; i < 10; i++) {
   (function (i) {
     nums[i] = function (j) {
         return i + j;
     };
   }(i));
 }
 }}}

 The specific code that was yielding function-within-loop errors is all
 avoiding this error, either by wrapping the relevant function definition
 in an IIFE or by keeping all function calls and references within the same
 loop context in which the function is defined. Because we're in an
 environment (the site front-end) where we want to keep file size down and
 therefore cannot reliably count on either Underscore or jQuery's
 iterators, I feel that in this file we're being responsible enough to mute
 these errors.

--
Ticket URL: <http://core.trac.wordpress.org/ticket/25970>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list