Getting a JSLint warning concerning labels in Javascript

一世执手 提交于 2019-12-01 20:45:23

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.

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
    }
}

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!