admin管理员组文章数量:1278880
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 ments for more details.
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 ments for more details.
- If you have to iterate a set of jQuery objects you can make it easy. You can find the first element that match a given predicate using .filter(...).first() – Adriano Repetti Commented Oct 11, 2012 at 21:58
5 Answers
Reset to default 13Returning 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.
本文标签: javascriptWhy does this function return true instead of falseStack Overflow
版权声明:本文标题:javascript - Why does this function return true instead of false - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741237903a2363386.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论