命名功能引數

CoffeeScript 允許在將物件和陣列作為引數提供給函式時對其進行解構。

利用解構的函式將在其簽名中指定其體內預期的所有欄位。呼叫此類函式時,必須將包含所有預期欄位的物件或陣列作為引數傳遞。

drawRect = ({x, y, width, height}) ->
  # here you can use the passed parameters
  # color will not be visible here!

myRectangle = 
  x: 10
  y: 10
  width: 20
  height: 20
  color: 'blue'

drawRect myRectangle
printTopThree = ([first, second, third]) ->
  # here you can use the passed parameters
  # 'Scrooge McDuck' will not be visible here!

ranking = ['Huey', 'Dewey', 'Louie', 'Scrooge McDuck']

printTopThree ranking