javascript - CoffeeScript: Setting default values for destructured arguments -



javascript - CoffeeScript: Setting default values for destructured arguments -

in coffeescript, can set default values function parameters so:

func = (one = 'one') ->

but how can set default values when object passed so:

func = ({ 1 }) ->

doing next won't work, instance:

func = ({ one: 'one' }) ->

i willing utilize external library lo-dash, provides _.defaults. can following:

func = (args) -> _.defaults args, { one: 'one' } { 1 } = args

but it's little long taste. particularly, i'd simplify one key/variable mentioned once. makes more useful when i'm changing function's parameters.

for instance, there method can use, perhaps in yet library (preferably 1 exists in lodash though, of course) destructure object using object's keys new variable names?

you have assign within function, unfortunately.

func =({one})-> 1 ?= 1 1 func(one: 2) #=> 2 func() #=> 1

using lowdash, on function call, of course

func _.defaults(ags, {one: 1})

depending on utilize case, acceptable.

and utilize partial application

func = ({one})-> 1 func1 = (fn, supplied)-> (args)-> fn(_.defaults(args, supplied)) myfun = func1(func, one: 1) myfun(foo: 'foo') #=> 1 myfun(one: 2) #=> 2

javascript function coffeescript underscore.js lodash

Comments

Popular posts from this blog

c - Compilation of a code: unkown type name string -

java - Bypassing "final local variable defined in an enclosing type" -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -