Javascript 关于javascript函数中的if语句的问题 - if语句返回true但函数返回false如何

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6183668/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 20:36:58  来源:igfitidea点击:

Question about if statement in function in javascript - how if the if statement return true but the function return false

javascriptfunction

提问by dramasea

can i ask you guys a question? Below is my code:

我可以问你们一个问题吗?下面是我的代码:

var num = 1;
var isNumberEqualOne = function(){
    if(num == 1){
      return true;
    }
    return false;
}();
alert(isNumberEqualOne);

In this code, the statement in the function return true, after return true, the code in the function is still executing right? So at the end, the code meet return false, so why the function still return true.Sorry for my bad english.Thanks

在这段代码中,函数中的语句返回true,返回true后,函数中的代码还在执行对吗?所以最后,代码满足返回false,那么为什么函数仍然返回true。对不起,我的英语不好。谢谢

回答by T.J. Crowder

As alex said, the returnfunction transfers control out of the function call immediately; no other statements in the function (other than finallyblocks) are executed.

正如亚历克斯所说,该return函数立即将控制权转移出函数调用;不finally执行函数中的其他语句(块除外)。

So:

所以:

function foo(a) {
    if (a == 1) {
        alert("a is 1");
        return;
        alert("This never happens, it's 'dead code'");
    }
    alert("a is not 1");
}
foo(1); // alerts "a is 1" and nothing else
foo(2); // alerts "a is not 1"

Regarding what I said above that "no other statements in the function (other than finallyblocks)are executed", more about finallyblocks:

关于我上面所说的“函数中没有其他语句(除了finally块)被执行”,更多关于finally块的信息:

function foo(a) {
    try {
        if (a == 3) {
            throw "a is e";
        }
        if (a == 1) {
            alert("a is 1");
            return;
            alert("This never happens, it's 'dead code'");
        }
        alert("a is not 1");
    }
    catch (e) {
        alert("exception: " + e);
    }
    finally {
        alert("finally!");
    }
}
foo(1); // alerts "a is 1", then "finally!"
foo(2); // alerts "a is not 1", then "finally!"
foo(3); // alerts "exception: a is 3", then "finally!"

Note that no matter how the execution leaves the try/catchblock, either naturally by falling out the bottom, or early because of return, or early because of an exception, the code in the finallyblock alwaysruns.

请注意,无论执行如何离开try/catch块,无论是自然掉到底部,还是因为 提前return,或者因为异常而提前,finally块中的代码始终运行。



Off-topic: Separately, note that you need parentheses around that function expression if you're going to call it immediately like that:

题外话:另外,请注意,如果您要像这样立即调用它,则需要在该函数表达式周围加上括号:

    var isNumberEqualOne = (function(){
//                         ^--- here
        if(num == 1){
           return true;
        }
        return false;
    })();
//   ^--- and here

or you can put the ()that call it within the parens like this:

或者您可以()像这样将调用它的放在括号中:

    var isNumberEqualOne = (function(){
//                         ^--- here
        if(num == 1){
           return true;
        }
        return false;
    }());
//     ^--- and here

Either works.

要么有效。

回答by alex

returnwill halt the function and return immediately. The remaining body of code in the function will notbe executed.

return将停止函数并立即返回。函数中剩余的代码体将不会被执行。

In your example, numis assigned 1, so the condition inside your function is true. This means that your function will return there and then with true.

在您的示例中,numisassigned 1,因此您的函数内的条件是true。这意味着您的函数将在那里返回,然后使用true.

You could also rewrite that function so its body is return (num == 1).

您也可以重写该函数,使其主体为return (num == 1).

回答by Alanyst

When a function executes a return statement, it will not continue executing statements that appear after it. So if num == 1evaluates to true, then the function will return true.

当函数执行 return 语句时,它不会继续执行出现在它后面的语句。因此,如果num == 1计算结果为true,则该函数将返回true

Note also that your alert statement is not calling the isNumberEqualOnefunction. You should do alert(isNumberEqualOne())if you want the function to be called.

另请注意,您的警报语句并未调用该isNumberEqualOne函数。alert(isNumberEqualOne())如果您希望调用该函数,您应该这样做。