Getting a JSLint warning concerning labels in Javascript

后端 未结 3 1976
余生分开走
余生分开走 2021-01-19 07:55

In my javascript I have this

    loopDeLoop:
        while (foo !== bar) {
            switch (fubar) {
                case reallyFubar:
                            


        
3条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-19 08:16

    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.

提交回复
热议问题