views:

581

answers:

4

In my javascript I have this

    loopDeLoop:
        while (foo !== bar) {
            switch (fubar) {
                case reallyFubar:
                    if (anotherFoo == anotherBar) {
                        break loopDeLoop;
                    }
                    break;
                default:
                    break;
            }
        }

But JSLint says... lint warning: use of label

Here's the notes from JSLint

Labels
JavaScript allows any statement to have a label, and labels have a separate name space. JSLint is more strict.

JSLint expects labels only on statements that interact with break: switch, while, do, and for. JSLint expects that labels will be distinct from vars and parameters.

How do I construct the above to get rid of the warning?

Thanks,
Greg

A: 

You could set a flag that determines whether or not you are done working in the loop.

var done = false;
while (foo !== bar && !done) {
    switch (fubar) {
        case reallyFubar:
            if (anotherFoo == anotherBar) {
                done = true;
            }
            break;

        default:
            break;
    }

    if(!done) {
        //If you have more logic inside the loop, put it here
    }
}
EndangeredMassa
A: 

Kinda hard to make a recommendation seeing as how the relationship between the two loops and the switch is left opaque in your example... But you could always just turn the implicit outer loop into an explicit outer loop, putting the inner loop in a closure:

while ( (function() // loop while function returns true
{
   while (foo !== bar) 
   {
      switch (fubar) 
      {
         case reallyFubar:
            if (anotherFoo == anotherBar)
            {
               return true; // back to outer loop
            }
            break;
         default:
            break;
       }
   }
   return false; // done looping
})()) {};

Before resorting to something as ugly as this, i'd probably try to factor out whatever algorithm is implicit in the inner loop as a separate function though.

Shog9
A: 

@Shog9 and @EndangeredMassa I appreciate the refactoring but I prolly didn't make myself clear in my question...I'm not concerned abt the construction of the loop...it's

How do I use a label in javascript?

It appears to me that I've constructed it correctly but JSLint throws the warning
"use of label"
So really my question is How do I properly use a label in Javascript? or to maybe be more precise...How do I use a label in Javascript so that JSLint does not throw a warning?...BTW...the code runs and works fine in the app...

w4ik
+4  A: 

You are using the label correctly.

JSLint is throwing a warning because labels in Javascript are horribly bad style, and JSLint wants you to know that.

To reiterate, if you use labels at all, even correctly, JSLint will give that warning.

Edit: Looks like you might be able to disable the label warning with a -use_of_label configuration directive.

Triptych
This has 2 downvotes. I wonder why...
Triptych
Probably a label lover.
Mark Rogers