命名功能参数

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