简体   繁体   English

当布尔函数返回false时,&&运算符和执行

[英]&& operator and execution when boolean function returns false

I don't understand a behavior of javascript. 我不明白javascript的行为。

I'm doing a form validation of a jquery ui dialog. 我正在对jquery ui对话框进行表单验证。 It seems then it's a javascript issue, not a jquery issue. 这似乎是一个javascript问题,而不是jquery问题。

For the validation, I execute a function per fields that return true or false and a boolean variable recieve the result of successive && operator. 对于验证,我为每个返回true或false的字段执行一个函数,一个布尔变量接收连续的&&运算符的结果。 Like this : 像这样 :

bValid = checkRegexp(validite, /^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$/, "Entrez la date de validit\350 sous la forme JJ/MM/AAAA." );
bValid = bValid && checkLength(libelle, "Libell\351", 1, 100 );
bvalid = bValid && checkLength(description, "Description", 1, 250);

Here are, for information, the validation functions : 以下是验证功能:

function checkLength( o, n, min, max ) {
    if ( o.val().length > max || o.val().length < min ) {
        o.addClass( "ui-state-error" );
        if(o.val().length == 0) { textError = textError + "le champ " + n + " est requis !\n"; }
        else { textError = textError + "Taille de " + n + " entre " + min + " et " + max + "caract\350res.\n"; }
        return false;
    } else {
        return true;
    }
}

function checkRegexp( o, regexp, n ) {
     if (!(regexp.test(o.val()))) {
         o.addClass( "ui-state-error" );
         textError = textError + n + "\n";
         return false;
     } else {
         return true;
     }
}

The behavior expected is that all the functions are executed and all the wrong fields are marked wrong with a concatenation of the error messages. 预期的行为是所有函数都被执行,并且所有错误的字段都标记为错误,并且错误消息的串联。 For information, the bValid variable contains the boolean result of the successive && operators. 有关信息,bValid变量包含连续的&&运算符的布尔结果。 This last point works ; 最后一点有效; no problemo. 没问题。

The real behavior is that when a function return false , the following functions don't seem to be executed. 真正的行为是当函数返回false ,似乎没有执行以下函数。 The result is that only the first wrong field met is marked wrong. 结果是只有遇到的第一个错误字段被标记为错误。

Why ? 为什么?

Because the && operator is 'short-circuited'. 因为&&运算符是“短路的”。 Meaning that since the compiler/interpreter/whatever knows that both sides operands for the && operator have to be true, if the first one is false then the second does not get executed. 这意味着,由于编译器/解释器/任何知道&&运算符的双方操作数必须为真,如果第一个操作数为假,则第二个操作符不会被执行。

If you want your function to execute for sure, just swap the order of the operands for && : 如果您希望函数执行肯定,只需交换&&的操作数顺序:

bValid = checkRegexp(validite, /^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$/, "Entrez la date de validit\350 sous la forme JJ/MM/AAAA." );
bValid = checkLength(libelle, "Libell\351", 1, 100 )     && bValid;
bvalid = checkLength(description, "Description", 1, 250) && bValid

JavaScript uses short circuit evaluation . JavaScript使用短路评估 ie false AND anything is always false, so why bother doing the 2nd computation? 即假,任何东西总是假的,所以为什么还要做第二次计算呢?

因为Javascript会简化和“优化”......如果已经是双&&操作的第一个操作数是假的,那么结果肯定是假的,所以它不会执行第二部分。

It's called short-circuiting. 它被称为短路。 The && can be considered logically as an "and-also" syntax where the second expression is not evaluated if the first expression fails. &&可以在逻辑上被视为“and-also”语法,如果第一个表达式失败,则不评估第二个表达式。 If you want the second expression to process regardless of the first, you may consider reversing their order or trying: 如果您希望第二个表达式无论第一个表达式如何处理,您可以考虑撤消其顺序或尝试:

bValid = bValid & checkLength(description, "Description", 1, 250);

Although, that is functionally equivalent to 虽然,这在功能上等同于

bValid = checkLength(description, "Description", 1, 250);

Let's reword this with actual output: 让我们用实际输出重写:

bValid = checkRegexp(validite, /^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$/, "Entrez la date de validit\350 sous la forme JJ/MM/AAAA." );
bValid = bValid && checkLength(libelle, "Libell\351", 1, 100 );
bvalid = bValid && checkLength(description, "Description", 1, 250);

false = checkRegexp(validite, /^(0?[1-9]|[12][0-9]|3[01])[\/\-](0?[1-9]|1[012])[\/\-]\d{4}$/, "Entrez la date de validit\350 sous la forme JJ/MM/AAAA." );
bValid = false && checkLength(libelle, "Libell\351", 1, 100 );
bvalid = false && checkLength(description, "Description", 1, 250);

The moment it hits false it'll stop executing that conditional. 它击中假的那一刻它将停止执行该条件。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM