Skip to content

Commit 360ba1f

Browse files
author
James Allardice
committed
Merge pull request #26 from artoale/patch-1
Pass i variable to IIFE
2 parents f3806a5 + 26e7175 commit 360ba1f

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

_posts/1909-01-01-dont-make-functions-within-a-loop.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ <h3>Why do I get this error?</h3>
5252
elems[i].addEventListener("click", function () {
5353
this.innerHTML = i;
5454
});
55-
}());
55+
}(i));
5656
}</textarea>
5757
<p>
5858
This will work as we expect it to, with each element getting the correct value of <span class="code">i</span>, but it's definitely starting to look messy and becoming harder to read (and therefore harder to maintain). And in any case, since there's still a function within the loop, JSLint and JSHint will still complain. To fix the issue, we need to move the function out of the loop, and maintain the closure:
@@ -74,4 +74,4 @@ <h3>Why do I get this error?</h3>
7474
}</textarea>
7575
<p class="standout">
7676
In JSHint 1.0.0 and above you have the ability to ignore any warning with a <a href="http://jshint.com/docs/#options" target="_blank">special option syntax</a>. The identifier of this warning is <strong>W083</strong>. This means you can tell JSHint to not issue this warning with the <code>/*jshint -W083 */</code> directive.
77-
</p>
77+
</p>

0 commit comments

Comments
 (0)