Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What do curly braces inside of function parameter lists do in es6?

People also ask

What do curly braces do in JavaScript?

Curly braces { } are special syntax in JSX. It is used to evaluate a JavaScript expression during compilation. A JavaScript expression can be a variable, function, an object, or any code that resolves into a value.

What is curly braces in node JS?

In javascript code, curly brackets are used to deconstruct an object.

What does empty curly braces mean in JavaScript?

It means the variable is a dictionary that stores key value pairs. The subscript or the value within the [] brackets is the key and the value on the right side is the value.

What type of brackets are used in conditions in JavaScript?

Using curly-left-bracket and curly-right-bracket makes conditionals clear.


It is destructuring, but contained within the parameters. The equivalent without the destructuring would be:

const func = o => {
    var param1 = o.param1;
    var param2 = o.param2;
    //do stuff
}

This is passing an object as a property.

It is basically shorthand for

let param1 = someObject.param1
let param2 = someObject.param2

Another way of using this technique without parameters is the following, let's consider then for a second that someObject does contain those properties.

let {param1, param2} = someObject;

It is an object destructuring assignment. Like me, you may have found it surprising because ES6 object destructuring syntax looks like, but does NOT behave like object literal construction.

It supports the very terse form you ran into, as well as renaming the fields and default arguments:

Essentially, it's {oldkeyname:newkeyname=defaultvalue,...}. ':' is NOT the key/value separator; '=' is.

Some fallout of this language design decision is that you might have to do things like

;({a,b}=some_object);

The extra parens prevent the left curly braces parsing as a block, and the leading semicolon prevents the parens from getting parsed as a function call to a function on the previous line.

For more info see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

Beware, key errors during object destructuring assignment do NOT throw; you just end up with "undefined" values, whether it's a key error or some other error that got silently propagated as 'undefined'.

> var {rsienstr: foo, q: bar} = {p:1, q:undefined};
undefined
> foo
undefined
> bar
undefined
>