study-note

引数

目次

デフォルト引数

greet = (name = "Guest") ->
  console.log "Hello, #{name}"

可変超引数(スプレッド引数)

sum = (nums...) ->
  total = 0
  for n in nums
    total += n
  total

nums...は任意の数の引数を配列として受け取る

引数の分割代入

showUser = ({name, age}) ->
  console.log "#{name} is #{age} years old"

サンプルコード

hello = (str = "World") ->
  console.log("Hello, #{str}")
hello()
hello('User')
addAll = (nums...) ->
  sum = 0
  for i in nums
    sum += i
  console.log sum
addAll 1, 2, 3
bookInfo = ({title, author}) ->
  console.log "title: #{title} author: #{author}"
book = {title: "Story", author: "John Smith"}
bookInfo(book)