{"id":162,"date":"2015-04-16T12:14:38","date_gmt":"2015-04-16T19:14:38","guid":{"rendered":"http:\/\/www.kolls.net\/blog\/?p=162"},"modified":"2015-04-16T12:14:38","modified_gmt":"2015-04-16T19:14:38","slug":"javascript-scoping-strikes-again","status":"publish","type":"post","link":"https:\/\/www.kolls.net\/blog\/?p=162","title":{"rendered":"JavaScript scoping strikes again"},"content":{"rendered":"<p>We have a for loop that populates an observable array in knockout.  For some reason, only the first item started being loaded, even though we hadn\u2019t made any changes to the loop, and all the data was still being sent down.<\/p>\n<p>The loop starts off:<\/p>\n<pre class=\"prettyprint\">for (i = 0; i &lt; ts.Weeks.length; i++) {<\/pre>\n<p>I stepped through the loop using Chrome debugger and found that i=0 consistently, all the way until the very end of the body, when we added the data object to the observable array:<\/p>\n<pre class=\"prettyprint\">self.Weeks.push(week);<\/pre>\n<p>At this point, it jumped to i=10 right before going back to the top.  Of course, with only a half-dozen data items, the loop then exited.  But, nothing else in the loop modifies i, and where was 10 coming from anyways??<\/p>\n<p>Since the array was observable, when it was updated with push, knockout also called all the various \u201ccomputed\u201d functions.  Also since the for loop variable \u201ci\u201d was declared without var, it was actually in the global scope!  One of those computed (I didn\u2019t figure out which one), or a library that they used, also had an \u201ci\u201d that was in the global scope, and the two variables were clobbering each other.<\/p>\n<p>Although I didn\u2019t determine the other perpetrator code, simply updating the loop, by adding var, to read<\/p>\n<pre class=\"prettyprint\">for (var i = 0; i &lt; ts.Weeks.length; i++) {<\/pre>\n<p>solved the issue by ensuring the variable i was function scoped.<\/p>\n<p>It\u2019s easy to forget in languages like C# that don\u2019t do global scope by default\u2026 JavaScript does do global scope by default!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We have a for loop that populates an observable array in knockout. For some reason, only the first item started being loaded, even though we hadn\u2019t made any changes to the loop, and all the data was still being sent &hellip; <a href=\"https:\/\/www.kolls.net\/blog\/?p=162\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[],"_links":{"self":[{"href":"https:\/\/www.kolls.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/162"}],"collection":[{"href":"https:\/\/www.kolls.net\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kolls.net\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kolls.net\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kolls.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=162"}],"version-history":[{"count":6,"href":"https:\/\/www.kolls.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/162\/revisions"}],"predecessor-version":[{"id":168,"href":"https:\/\/www.kolls.net\/blog\/index.php?rest_route=\/wp\/v2\/posts\/162\/revisions\/168"}],"wp:attachment":[{"href":"https:\/\/www.kolls.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=162"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kolls.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=162"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kolls.net\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}