简体   繁体   中英

What do curly braces do in a function call (as argument, NOT a parameter) in Javascript

So I have to work with a function call like this:

someFunction({ someVar });

My question is what do the curly braces do, why use them?

It passes an object as the argument. The object has one property named 'someVar' which has the value of the someVar variable.

 const someVar = 'foo'; console.log({ someVar });

When you include curly braces in a function call as argument it passes an object as the argument and the key is same as the name of the variable and value as the value stored in the variable. See below snippet for the reference.

 function func(argument) { console.log(argument) } const variable = "value" func({variable})

you should use this type of argument if you want to pass some value with some unique identifier as key in the function. this type of argument is widely used in reactjs for passing props in the functional components.

please upvote if you find the answer useful.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM