Closed gromgit closed 8 years ago
That's really cool, great addition! I would love to see this being merged.
Hello @gromgit !
Just one typo mistake :
var sayHelloTimes2 = function({ name = "Anony", surname = "Moose" } = {}, times) {
console.log(`Hello ${name} ${surname}! I've seen you ${times} times before.`);
};
sayHelloTimes({ name: "Pam" }, 5678) //SHOULD BE : sayHelloTimes2({ name: "Pam" }, 5678)
// -> Hello Pam Moose! I've seen you 5678 times before.
sayHelloTimes2(5678)
// -> Hello Anony Moose! I've seen you undefined times before.
Also, I'm not sure why you choose { name = "Anony", surname = "Moose" } = {}
instead of simply { name = "Anony", surname = "Moose" }
for the first argument in the sayHelloTimes2
definition?
It doesn't seems to change anything so I was wondering if there was a reason ?
Also, :shipit:
Yes .. I too wondering about that .. First argument.. What that extra ={} will do.. ? First of all.. Is that right? If right .. What it will do.. Can you please explain?
@adaniloff Ouch, thanks for the typo catch!
@shivarajnaidu As explained in the text, = {}
denotes the default value for the entire parameter object. It seems confusing because I forgot to include one set of examples, where the expected object parameter is completely missing. As you can see from my latest update, the results with and without = {}
are very different, in one case potentially fatal to your code.
Thanks @gromgit ;). Merged.
The original tip mentions options objects, for which it's almost always necessary to handle missing properties with default values. I show how this can be done in a very neat way.
Also added examples to show what happens when incorrect, incomplete or missing arguments are passed with/without default parameters.
@dislick