coffeescript Named Function Parameters


Example

CoffeeScript allows to deconstruct objects and arrays when they are fed to functions as arguments.

A function that leverages deconstruction will specify in its signature all the fields that are expected within its body. When invoking such function, an object or array containing all the expected fields has to be passed as argument.

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