1
function lookupRecord2({value1 = "blue", value2 = 7}) {
        console.log('theValues');
        console.log(value1);
        console.log(value2);
}

lookupRecord2( { value1: 'pink', value2: 9 } );

I've read that Javascript doesn't allow for named parameters so I'm confused how the above code works. What is happening here that's allowing me to override the params?

I think what's confusing is that the params are assigned with "=" whereas the object I'm passing uses ":" ...

ADDED:

I know this is a form of destructuring, but I still can't make sense of it b/c it's in the function declaration.

Gary
  • 909
  • 9
  • 26
  • 1
    Looks like destructured assignment. – user202729 Aug 02 '20 at 01:39
  • https://stackoverflow.com/questions/26578167/es6-object-destructuring-default-parameters?noredirect=1&lq=1 ? – user202729 Aug 02 '20 at 01:40
  • Thank you, I found examples like this but it has a {} = {} declaration, which seems different. – Gary Aug 02 '20 at 01:42
  • Perhaps this one. [javascript - can someone explain this seemingly weird assignment `{key=value} = argument` - Stack Overflow](https://stackoverflow.com/questions/52801891/can-someone-explain-this-seemingly-weird-assignment-key-value-argument?noredirect=1&lq=1) – user202729 Aug 02 '20 at 01:45
  • Thank you, that post led me here: https://stackoverflow.com/questions/26578167/es6-object-destructuring-default-parameters ... which answers the question! Big help! – Gary Aug 02 '20 at 01:47

1 Answers1

0
function des({ a = 1, b = 2 } = {}) {
        console.log(a);
        console.log(b);
}

des();

This is a destructing syntax where we're able to pass default values. It works everywhere, not just in function declarations.

{ a = 1, b = 2 } = { b:3 }
// assigns 1 to "a" and because "b" already exists, "b" is 3

MORE INFO HERE: can someone explain this seemingly weird assignment `{key=value} = argument`

Gary
  • 909
  • 9
  • 26