Comments on: Dirty looping in JavaScript http://pierrespring.com/2010/05/24/dirty-looping-in-javascript/ geek, punk, pussy … Sat, 05 Oct 2013 14:25:34 +0000 hourly 1 http://wordpress.org/?v=3.8.12 By: Jose http://pierrespring.com/2010/05/24/dirty-looping-in-javascript/comment-page-1/#comment-34892 Fri, 30 Nov 2012 03:26:29 +0000 http://pierrespring.com/?p=78#comment-34892 Good one! Hoisting is a new concept to me, you helped clarify it. This page is good too:

http://www.programmerinterview.com/index.php/javascript/javascript-hoisting/

]]>
By: Ivan Krechetov http://pierrespring.com/2010/05/24/dirty-looping-in-javascript/comment-page-1/#comment-116 Tue, 17 Aug 2010 13:54:18 +0000 http://pierrespring.com/?p=78#comment-116 Nice tip, thanks! I also enjoyed the previous posts.

I guess, another fun thing to do is passing parameters in this nested loop’s function:


(function(j){
    …
})(i);

Thus, the enclosing loop’s i becomes a j in the nested loop.

]]>
By: Pierre Spring http://pierrespring.com/2010/05/24/dirty-looping-in-javascript/comment-page-1/#comment-23 Thu, 27 May 2010 12:17:35 +0000 http://pierrespring.com/?p=78#comment-23 @BOobby: the problem with let is that the scope of a variable declared inside a let statement using var is still the same as if it had been declared outside the let statement – such variables still have function scoping.

In the above example, var row; would still be in the scope of the whole function.

And of course, let only runs in FF >= 3.0 …

]]>
By: BObby http://pierrespring.com/2010/05/24/dirty-looping-in-javascript/comment-page-1/#comment-22 Thu, 27 May 2010 10:28:25 +0000 http://pierrespring.com/?p=78#comment-22 Or you could use the let keyword in JavaScript 1.7+, but of course there are problems with this too.

]]>
By: Pierre Spring http://pierrespring.com/2010/05/24/dirty-looping-in-javascript/comment-page-1/#comment-21 Thu, 27 May 2010 06:42:39 +0000 http://pierrespring.com/?p=78#comment-21 Freeman: The point I am trying to make is not to get the enclosing scope of a loop dirty. i and row declaration both get hoisted to the top if you don’t wrap the loop in a function. And this can have unexpected results.

The assure_positive() is for educational purpose and therefor it is kept very simple.

]]>
By: A Freeman http://pierrespring.com/2010/05/24/dirty-looping-in-javascript/comment-page-1/#comment-20 Thu, 27 May 2010 00:21:41 +0000 http://pierrespring.com/?p=78#comment-20 Why would you reuse the outer loop index for the inner loop, in the first place? Am I missing something here?

for (i=0;i<matrix.length;i++) {
    var row=matrix[i];
    for (j=0;j<row.length;j++) {
        // stuff happens here
    }
}
]]>
By: Pierre Spring http://pierrespring.com/2010/05/24/dirty-looping-in-javascript/comment-page-1/#comment-19 Tue, 25 May 2010 08:07:09 +0000 http://pierrespring.com/?p=78#comment-19 @jordi: Where you put the closing parenthesis does not matter really, it is a question of convention. Crockford demands that you close it the way it’s done in my examples, arguing that the way you do it makes no sense (c.f. video 3 at 29’28”)

To me both make perfect sense, yet I choose to adhere to the standards proposed by Crockford.

The only important thing is to have the opening parenthesis, in order to not to have a function expression rather than a function statement. The statement would not be callable.

]]>
By: Jordi Boggiano http://pierrespring.com/2010/05/24/dirty-looping-in-javascript/comment-page-1/#comment-18 Mon, 24 May 2010 14:56:20 +0000 http://pierrespring.com/?p=78#comment-18 Interesting method. Just a note on using one-shot functions as you did, I usually saw the function wrapped in parenthesis’ and then executed, rather than the execute then wrap that you have there, so that would look like:

(function(){
    //stuff
})();

I’m not sure if there is a difference between the two as far as browser support goes or anything, but if the other induces errors, you may want to try that.

]]>