#Objects behaves differently when inside function parameters

4 messages · Page 1 of 1 (latest)

devout token
#

export const setCookie = ({ email, login = true }) => { if (email !== undefined) { document.cookie = 'email=' + email; } document.cookie = 'login=' + login; }

how is an '=' sign allowed in an Object?
why did it work? {email, login=true} this is wrong but foo({email, login=true}) this is right. Why?
Does javaScript magically understands like Ok so the object is inside of a function parameter so behave differently

marble nacelle
#

Generally speaking they're treated equally and both work. Here's a scrim with a simplified example showing both traditional and arrow functions working:

https://scrimba.com/scrim/cbmDm7Hg

wild hull
# devout token `export const setCookie = ({ email, login = true }) => { if (email !== undef...

The expression that is wrapped in curly braces isn't an actual object definition, it's object destructuring syntax.

That code is basically saying that the arrow function will accept an Object as a parameter, and when the function is called, JavaScript should create function-scope variables named email and login from the parameter object's email and login properties. And if the parameter object has no login property, the login variable is assigned a default value of true. Does this make sense?

devout token