Default function parameter ordering

后端 未结 3 353
旧时难觅i
旧时难觅i 2021-01-30 08:28

Reading through this, I came to the bit on default values for function arguments:

fill = (container, liquid = \"coffee\") ->
  \"Filling the #{container} with         


        
3条回答
  •  迷失自我
    2021-01-30 09:19

    fill = ({container, liquid} = {}) ->
         container ?= "mug"
         liquid ?= "coffee"
    
         "Filling the #{container} with #{liquid}..."
    
    alert fill(liquid: "juice", container: "glass")
    alert fill()
    
    fill = (quantity="500 mL", {container, liquid} = {}) ->
         container ?= "mug"
         liquid ?= "coffee"
    
         "Filling the #{container} with #{quantity} of #{liquid}..."
    
    alert fill("1L", liquid: "juice", container: "glass")
    alert fill()
    alert fill "1L"
    alert fill "1L", liquid: "water"
    

提交回复
热议问题