【问题标题】:Using the || operator in the a return value of a function [duplicate]使用 ||函数的返回值中的运算符[重复]
【发布时间】:2016-01-14 13:30:35
【问题描述】:

我试图在http://eloquentjavascript.net/03_functions.html 免费在线书籍的第 3 章中理解这个示例。我对 || 的使用感到困惑if 语句中最后一个 else 的返回函数中的运算符。

这里是代码

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 ||运算符。

-------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)

我的问题是创建跳转到 elseBlocks return 语句的另一部分的原因,即 || 之后的 * 3而不是+ 5。是不是因为之前的返回为null。还是因为 start 现在 > 而不是 target。

提前致谢。

【问题讨论】:

  • 这是因为 elseIfBlock 总是返回 null,所以 null || func() 导致调用 func()

标签: javascript function recursion


【解决方案1】:

|| 是一个logical operator。这意味着如果它可以转换为true,它将返回第一个表达式;否则,它返回第二个表达式。

由于逻辑表达式是从左到右计算的,因此使用以下规则测试它们是否存在可能的“短路”计算:

  • false && (anything) 短路评估为 false。
  • 真|| (anything) 短路评估为真。

(MDN)

所以在下面的 sn-p 中,如果第一个 find() 的计算结果不是真值,那么它将执行并返回第二个 find()

return find(start + 5, "(" + history + " + 5)") ||
       find(start * 3, "(" + history + " * 3)");

【讨论】:

  • 好吧,假设它在第一次find(1, "1") 之后的第二次通过时运行find(6 , "( 1 +5 )"),那么第二次通过是真的。我希望我没有让你感到困惑。我只是不明白为什么它是真还是假。
  • 是不是因为没有return null
  • 第二遍不一定是真的。只有第一遍必须为真才能返回。如果你把它想象成人类的语言,它就是在说“如果它是真的就返回这个东西,否则不管它是不是真的都返回这个东西。”我希望我能正确理解您的要求。
  • 我想是的。基本上使用第一个 find() 函数调用,除非返回 !true ,例如 null 然后运行另一个 || find() 函数。
猜你喜欢
  • 2013-07-31
  • 1970-01-01
  • 1970-01-01
  • 2013-05-26
  • 2014-04-02
  • 2011-05-27
  • 1970-01-01
  • 2019-09-04
  • 1970-01-01
相关资源
最近更新 更多