How to call outer function's return from inner function?

后端 未结 4 1014
抹茶落季
抹茶落季 2020-12-15 03:05

I have such code:

function allValid() {
    $(\'input\').each(function(index) {
        if(something) {
            return false; 
        }    
    });

            


        
4条回答
  •  余生分开走
    2020-12-15 03:51

    If you want to do this efficiently, I think this is the best way:

    function allValid() {
      elements = $('input')
      for (i = 0; i < elements.length; i++) { invalidityCheck(elements[i]) && return false; }
      return true;
    }
    

    Edit: Although a more JavaScript-y version would probably use exceptions:

    function allValid() {
      try
        $('input').each(function(index)) {
          if (something) { throw 'something happened!'; }
        });
      catch (e) {
        if (e == 'something happened!') {
          return false;
        } else {
          throw e;
        }
      }
      return true;
    }
    

提交回复
热议问题