2015-10-15 29 views
1

我想了解http://eloquentjavascript.net/03_functions.html免費在線書第3章中的這個示例。我對||的使用感到困惑運算符在if語句中的else 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返回語句的其他部分* 3之後的||。而不是+5。是否因爲之前的返回值爲空。或者是因爲現在開始>而不是目標。

在此先感謝。

+0

這是因爲* elseIfBlock *總是返回null,所以'null || func()'會調用'func()' – Amit

回答

1

||logical operator。這意味着如果它可以轉換爲true,它將返回第一個表達式;否則,它返回第二個表達式。

  • 假& &(任何)是短路評價爲false:

    作爲邏輯表達式進行求值從左到右,它們是爲可能的「短路」的評價使用以下規則測試。

  • true || (任何事物)都被短路評估爲真。

MDN

下面的代碼片段

因此,如果第一find()不計算爲一個truthy值,那麼它會執行並返回第二find()

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

ok,所以假設它在第一次find(1,「1」)之後的第二遍中運行find(6,「(1 + 5)」) 「這第二遍是怎麼回事?我希望我不會讓你困惑。我只是不明白爲什麼它是真實的與虛假的。 –

+0

是不是因爲它不返回空格 –