javascript - Boolean Concatenation? What's the real term for this pattern? - Stack Overflow

Please consider the following function body:var isValidated = true;$(selector1).each(function(){do v

Please consider the following function body:

var isValidated = true;
$(selector1).each(function(){
    //do validation with local f()...
    isValidated = f() && isValidated;
});

$(selector2).each(function(){
    //do validation with local f()...
    isValidated = f() && isValidated;
});

$(selector3).each(function(){
    //do validation with local f()...
    isValidated = f() && isValidated;
});

return isValidated;

My description of the progress of isValidated is Boolean concatenation---but there has to be an official, puter-science term for this thing. What is it? To clarify, the idea here is to let each $() block run---but when any one of these blocks have a validation failure the results of this failure must return false over all blocks (true && true && false == false). So, like many programmers, I am using some kind of a pattern but we often don't know what it is called. So does this pattern resemble anything useful?

Please consider the following function body:

var isValidated = true;
$(selector1).each(function(){
    //do validation with local f()...
    isValidated = f() && isValidated;
});

$(selector2).each(function(){
    //do validation with local f()...
    isValidated = f() && isValidated;
});

$(selector3).each(function(){
    //do validation with local f()...
    isValidated = f() && isValidated;
});

return isValidated;

My description of the progress of isValidated is Boolean concatenation---but there has to be an official, puter-science term for this thing. What is it? To clarify, the idea here is to let each $() block run---but when any one of these blocks have a validation failure the results of this failure must return false over all blocks (true && true && false == false). So, like many programmers, I am using some kind of a pattern but we often don't know what it is called. So does this pattern resemble anything useful?

Share Improve this question edited Nov 6, 2009 at 20:29 Ateş Göral 140k27 gold badges141 silver badges191 bronze badges asked Nov 6, 2009 at 20:09 rasxrasx 5,3482 gold badges48 silver badges62 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 10

Boolean Logic. (and look at the "chaining operations" section).

Not sure if this is a named pattern per se, but I just wanted to point out a minor observation... Since you are just returning boolean, it would be better to switch your checks so that after the first failure the logic will simply short circuit as false without running additional functions unnecessarily:

isValidated = isValidated  && f();

The only reason to ensure you still run every f() is if they are doing something like marking UI fields with errors for the user. If they are simply validating, no need to run them once the outer check is false.

I'm assuming from your use of .each() that you also have access to an .inject() function, which is what I'd remend using when you're building a single value from an enumerable.

function validated(a) {
  return a.inject(true, function(acc, el){ return(acc && f()); });
}

return validated($(selector1)) && validated($(selector2)) && validated($(selector3));

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743716372a4495058.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信