I'm looking at someone else's functioning Javascript code. Why are there curly brackets in the parameters when declaring a function? eg:
function createUser({username, password, name, weight}, f) {};
Is this just enforcing and renaming keys that would be in a passed in object? This is in model.js so perhaps it has something to do with validation?
Follow up questions: How can I get this not to error out when I try to compile this on my machine? I get "SyntaxError: Unexpected token {" at the first of these strange brackets.
It's an ES6 destructuring assignment.
This syntax declares a function with two parameters.
The values of the username
, password
, name
and weight
properties of the object passed as the first argument will be available through the variables username
, password
, name
and weight
in the function body.
The second argument will be available through the variable f
.
For example:
(function ({a,b}, c) {
return [a,b,c];
})({a:1, b:2, d:"ignored"}, 3); // [1,2,3]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With