使用||函数的返回值中的运算符 [英] Using the || operator in the a return value of a function
问题描述
我试图理解 http://eloquentjavascript.net/03_functions.html 免费在线图书.我对||的使用感到困惑if语句中最后else的返回函数中的运算符.
I am trying to understand this example in chapter 3 of the http://eloquentjavascript.net/03_functions.html free online book. I am confused about the use of || operator in the return function of the final else in a if statement.
这是代码
function findSolution(target) {
function find(start, history) {
if (start == target){
console.log("-------ifBlock-------");
console.log("startInteger = " + start + " == targetInteger = " + target + " historyString " + history);
return history;
}
else if (start > target){
console.log("-------elseIfBlock-------");
console.log("startInteger = " + start + " > targetInteger = " + target + " historyString " + history);
return null;
}
else{
console.log("-------elseBlock-------");
console.log("startInteger = " + start + " historyString = " + history);
return find(start + 5, "(" + history + " + 5)") ||
find(start * 3, "(" + history + " * 3)");
}
}
return find(1, "1");
}
findSolution(24);
这是我尝试尝试并了解返回||的流程的所有console.logs.运算符.
Here is all the console.logs I have been getting to try and understand the flow of the return || operator.
-------elseBlock-------
startInteger = 1 historyString = 1
-------elseBlock-------
startInteger = 6 historyString = (1 + 5)
-------elseBlock-------
startInteger = 11 historyString = ((1 + 5) + 5)
-------elseBlock-------
startInteger = 16 historyString = (((1 + 5) + 5) + 5)
-------elseBlock-------
startInteger = 21 historyString = ((((1 + 5) + 5) + 5) + 5)
-------elseIfBlock-------
startInteger = 26 > targetInteger = 24 historyString = (((((1 + 5) + 5) + 5) + 5) + 5)
-------elseIfBlock-------
startInteger = 63 > targetInteger = 24 historyString = (((((1 + 5) + 5) + 5) + 5) * 3)
-------elseIfBlock-------
startInteger = 48 > targetInteger = 24 historyString = ((((1 + 5) + 5) + 5) * 3)
-------elseIfBlock-------
startInteger = 33 > targetInteger = 24 historyString = (((1 + 5) + 5) * 3)
-------elseBlock-------
startInteger = 18 historyString = ((1 + 5) * 3)
-------elseBlock-------
startInteger = 23 historyString = (((1 + 5) * 3) + 5)
-------elseIfBlock-------
startInteger = 28 > targetInteger = 24 historyString = ((((1 + 5) * 3) + 5) + 5)
-------elseIfBlock-------
startInteger = 69 > targetInteger = 24 historyString = ((((1 + 5) * 3) + 5) * 3)
-------elseIfBlock-------
startInteger = 54 > targetInteger = 24 historyString = (((1 + 5) * 3) * 3)
-------elseBlock-------
startInteger = 3 historyString = (1 * 3)
-------elseBlock-------
startInteger = 8 historyString = ((1 * 3) + 5)
-------elseBlock-------
startInteger = 13 historyString = (((1 * 3) + 5) + 5)
-------elseBlock-------
startInteger = 18 historyString = ((((1 * 3) + 5) + 5) + 5)
-------elseBlock-------
startInteger = 23 historyString = (((((1 * 3) + 5) + 5) + 5) + 5)
-------elseIfBlock-------
startInteger = 28 > targetInteger = 24 historyString = ((((((1 * 3) + 5) + 5) + 5) + 5) + 5)
-------elseIfBlock-------
startInteger = 69 > targetInteger = 24 historyString = ((((((1 * 3) + 5) + 5) + 5) + 5) * 3)
-------elseIfBlock-------
startInteger = 54 > targetInteger = 24 historyString = (((((1 * 3) + 5) + 5) + 5) * 3)
-------elseIfBlock-------
startInteger = 39 > targetInteger = 24 historyString = ((((1 * 3) + 5) + 5) * 3)
-------ifBlock-------
startInteger = 24 == targetInteger = 24 historyString = (((1 * 3) + 5) * 3)
我迷路的地方恰好是 else if(start> target){}
块的起点.执行该代码后,将要求其返回null.然后 historyString =(((((1 + 5)+ 5)+ 5)+ 5)+ 5)
.
Where I get lost is right where the else if (start > target){}
block starts. When that code gets executed it is then asked to return null. And at that point the historyString = (((((1 + 5) + 5) + 5) + 5) + 5)
.
我的问题是什么导致跳转到elseBlocks返回语句||之后的* 3的另一部分.而不是+5.是否是因为先前的返回为空.还是因为开始现在比目标开始要早.
预先感谢.
推荐答案
||
is a logical operator. This means that it will return the first expression if it can be converted to true; otherwise, it returns the second expression.
当逻辑表达式从左到右求值时,将使用以下规则对它们进行可能的短路"求值测试:
As logical expressions are evaluated left to right, they are tested for possible "short-circuit" evaluation using the following rules:
- 假&&(任何东西)被短路评估为假.
- true ||(任何东西)短路评估为true.
( MDN )
因此,在以下代码段中,如果第一个 find()
的求值结果不是真实值,则它将执行并返回第二个 find()
.
So in the following snippet, if the first find()
doesn't evaluate to a truthy value, then it will execute and return the second find()
.
return find(start + 5, "(" + history + " + 5)") ||
find(start * 3, "(" + history + " * 3)");
这篇关于使用||函数的返回值中的运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!