简体   繁体   English

为什么此函数返回true而不是false

[英]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. 我希望test函数返回false,但返回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. .each()回调返回false只会暂停.each()迭代。 It doesn't return from the enclosing function; 它不会从封闭函数返回; the only way to do that in JavaScript is to throw an exception. 在JavaScript中执行此操作的唯一方法是引发异常。

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. 它返回true因为内部return false返回匿名函数,该函数仅指示jQuery尽早结束$.each循环。

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: 如果只需要for...in循环就足够了,还可以更改代码以不使用$.each方法:

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; 那是因为return false; just breaks out of the $.each loop.. but not the function. 只是打破了$ .each循环..但没有功能。

It returns true from the last statement 从最后一条语句返回true

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 内部函数(在$.each ):返回false
  • Outer Function ( window.test ): returns true 外部函数( window.test ):返回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'; 然后在外部函数中,您可以执行类似return typeof breakpoint !== 'undefined'; , or set a returnValue as others have suggested. ,或按照其他建议设置returnValue

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么此.every()函数返回true然后返回false? - Why does this .every() function return true and then false? 为什么“[] == 0”返回true而“[]”为真且“0”为假? - why does “[] == 0” return true while “[]” is true and “0” is false? 为什么lodash`_.all([true,true,true],true);`return`false`? - Why does lodash `_.all([true, true, true], true);` return `false`? 为什么这个函数只返回真,因为没有 else 语句使它在非真条件下返回假? - Why does this function return only true, since there is no else statement to make it return false under a non true condition? 为什么我的 `if(result !== &quot;false&quot;)` 返回 true? - Why does my `if(result !== "false")` return true? MongoDB / Express:为什么Array.includes返回false而不是true? - MongoDB/Express: Why does Array.includes return false instead of true? 为什么这个函数返回true? - Why does this function return true? 为什么JS中的true ==&#39;true&#39;语句返回false? - Why does true == 'true' statement in JS return false? 为什么0 &lt;undefined返回false而不是undefined? - Why does 0 < undefined return false instead of undefined? 为什么“0 &amp;&amp; true”在javascript中返回0而不是布尔值? - Why does “0 && true” return 0 in javascript instead of a boolean?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM