LoginSignup
0
0

More than 5 years have passed since last update.

CoffeeScript ベタ書きでオプション展開処理をしたい

Last updated at Posted at 2013-07-01

関数にオプションを渡す際の処理を、メソッドを作らないでも簡潔に書けないかを模索中です。

func = (options={}) ->
  opts =
    x: 1
    y: 2
    z: 3
  opts[k] = v for k, v of options

  console.log opts

func({x:11, z:33})  # -> {x: 11, y: 2, z: 33}

良い書き方ありませんでしょうか?

結局は extend を作ったほうが良い

shallow copy も出来ないので、概ねは作ったほうが良かった

extend = (obj, props) ->
  obj[k] = v for k, v of props
  obj
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0