Skip to content

Instantly share code, notes, and snippets.

@taiansu
Created June 20, 2012 17:35
Show Gist options
  • Save taiansu/2961086 to your computer and use it in GitHub Desktop.
Save taiansu/2961086 to your computer and use it in GitHub Desktop.
More about JavaScript & CoffeeScript arguments
motorcycle = ({ brand, year } = { brand: 'Unknown', year: "1999" }) ->
alert "brand: #{brand}, year: #{year}"
motorcycle()
#brand: 'Unknown', year: "1999"
motorcycle year:"2005", brand:"YAMAHA"
#brand: 'YAMAHA', year: 2005
#Section I
run_from_killer = (survivors..., victim)->
console.log "dead: #{victim}, survive: #{survivors}"
run_from_killer "John"
#dead: John, survive:
run_from_killer "John","Bob","Alex","Sam"
#dead: Sam, survive: John,Bob,Alex
#Section II
fruit = (first, middle..., last) ->
console.log "first: #{first}, last: #{last}, middle: #{middle}"
fruit "apple", "banana"
#first: apple, last: banana, middle:
fruit "apple", "banana", "coconut", "dragon fruit"
#first: apple, last: dragon fruit, middle: banana,coconut
#Section III
my_cargo = ["cherry","pear","papaya","orange","mango"]
fruit my_cargo...
#first: cherry, last: mango, middle: pear,papaya,orange
#first: apple, last: dragon fruit, middle: banana,coconut
function showName(name){
console.log(name.first + " " + name["last"] + " "+ name["middle"]);
}
showName({"last" : "Lee",
"first" : "John"})
//John Lee undefined
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment