[英]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: 您有两个函数定义:
$.each
): returns false $.each
):返回false 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.