I have such code:
我有这样的代码:
function allValid() {
$('input').each(function(index) {
if(something) {
return false;
}
});
return true;
}
which always returns true as return false;
affects anonymous inner function. Is there an easy way to call outer function's return?
它总是返回true作为返回false;影响匿名内部的功能。是否有一种简单的方法来调用外部函数的返回值?
PS. I am not looking for a workaround, just want to know the answer to original question. If the answer is "not possible" it is fine.
我并不是在寻找解决办法,我只是想知道原始问题的答案。如果答案是“不可能”,那也可以。
4 个解决方案
#1
17
Yeah, store it in a local variable.
将它存储在一个局部变量中。
function allValid() {
var allGood = true;
$('input').each(function (index) {
if (something) {
allGood = false;
}
});
return allGood;
}
#2
5
You can also do this with filter:
你也可以用过滤器来做:
var anyInvalid = $('input').filter(function(index) {
if(inValidCheck)
return true;
}).length;
This works because 0 is treated as false, but it actually gives you the number of invalid, which you could use this to display "You have 3 invalid entries" or something if you wanted.
这是有效的,因为0被认为是假的,但是它实际上给了你无效的数字,你可以用它来显示“你有3个无效的条目”或者其他你想要的东西。
#3
1
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:
编辑:虽然更多的javascript版本可能会使用异常:
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;
}
#4
1
You could also use Array.prototype.some which iterates until finding an element that matches the criteria.
你也可以使用Array.prototype。一些迭代,直到找到匹配条件的元素。
function allValid() {
var inputs = $('input');
if(inputs.toArray().some(function(input){
if(something)
return true;
})) {
return false;
} else {
return true;
}
}
#1
17
Yeah, store it in a local variable.
将它存储在一个局部变量中。
function allValid() {
var allGood = true;
$('input').each(function (index) {
if (something) {
allGood = false;
}
});
return allGood;
}
#2
5
You can also do this with filter:
你也可以用过滤器来做:
var anyInvalid = $('input').filter(function(index) {
if(inValidCheck)
return true;
}).length;
This works because 0 is treated as false, but it actually gives you the number of invalid, which you could use this to display "You have 3 invalid entries" or something if you wanted.
这是有效的,因为0被认为是假的,但是它实际上给了你无效的数字,你可以用它来显示“你有3个无效的条目”或者其他你想要的东西。
#3
1
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:
编辑:虽然更多的javascript版本可能会使用异常:
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;
}
#4
1
You could also use Array.prototype.some which iterates until finding an element that matches the criteria.
你也可以使用Array.prototype。一些迭代,直到找到匹配条件的元素。
function allValid() {
var inputs = $('input');
if(inputs.toArray().some(function(input){
if(something)
return true;
})) {
return false;
} else {
return true;
}
}