-1

I found some codes when reading a article. And i can't understand it. But when i tested it, it worked! can anyone told me what is parameter means? it is a destructuring?

function test ({ a = '1', b = '2', c = '3' } ={}) {
  console.log(a, b, c);
};
test();  //> "1" "2" "3"
Nick Parsons
  • 45,728
  • 6
  • 46
  • 64

2 Answers2

1

Here default values for properties are set: { a = '1', b = '2', c = '3' }. As soon as no value is passed to test() - default value {} is used. It doesn't have those properties a, b, c - so resulted variables get default values.

In case you pass some object with existing known properties - their values will be used:

function test ({ a = '1', b = '2', c = '3' } ={}) {
  console.log(a, b, c);
};
test();  //> "1" "2" "3"

test({a: 10});  //> "10" "2" "3"

You can read more about destructuring on MDN.

falinsky
  • 7,229
  • 3
  • 32
  • 56
0

Yes I get it now.

1, { a = '1', b = '2', c = '3' } : object {a,b,c} have default values '1','2','3'.

2, {}: this is default parameter.

3, { a = '1', b = '2', c = '3' }={} : it will destructure the parameter to object {a,b,c}. then get values of variables a,b,c

thanks falinsky.