简体   繁体   中英

Why does this function return true instead of false

Here is my test:

var test = function () {
    $.each([1, 2], function () {
        if(true !== false) { // it is just an example
            alert('I am here'); 
            return false; // how should I make the function test to stop or to exit here?
        }
    });
    return true;
}​;

alert(test());

I would like the test function to return false but it returns true.
Why? How should I fix the code? Please see the comments for more details.

Returning false from the .each() callback just halts the .each() iteration. It doesn't return from the enclosing function; the only way to do that in JavaScript is to throw an exception.

What you could do is set a flag:

var test = function () {
    var abort = false;
    $.each([1, 2], function () {
        if(true !== false) { // it is just an example
            alert('I am here'); 
            abort = true;
            return false; // how should I make the function test to stop or to exit here?
        }
    });
    return !abort;
}​;

It returns true because the inner return false returns the anonymous function, which only instructs jQuery to end the $.each loop early.

Use a variable outside the inner function to properly juggle the return status.

var test = function () {
    var retVal = true;
    $.each([1, 2], function () {
        if(true !== false) { // it is just an example
            alert('I am here'); 
            retVal = false;
            return false;
        }
    });
    return retVal;
}​;

You could also change your code to not use the $.each method, if a simply for...in loop would suffice:

var test = function () {
    var retVal = true;
    for (var value in [1, 2]) {
        if(true !== false) { // it is just an example
            alert('I am here'); 
            return false;
        }
    };
    return true;
};

That is because return false; just breaks out of the $.each loop.. but not the function.

It returns true from the last statement

fix it by changing your code to:

var test = function () {
    var returnValue = true;
    $.each([1, 2], function () {
        if(true !== false) { // it is just an example
            alert('I am here'); 
            returnValue = false;
            return false; // how should I make the function test to stop or to exit here?
        }
    });
    return returnValue;
}​;

You have two function definitions:

  • Inner Function (in $.each ): returns false
  • Outer Function ( window.test ): returns true

Capture when exited:

var arr = [1,2,3,4,5,6,7,8,9];
var breakpoint = undefined;
$.each(arr, function(i,val){
   if (val==4){  // some break condition
      breakpoint = {index:i,val:val};
      return false;
   }
   return true;
});

console.log('break point:', breakpoint); // breakpoint.index=3, breakpoint.val=4

Then in your outer function you can do something like return typeof breakpoint !== 'undefined'; , or set a returnValue as others have suggested.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM