A small problem with js

I just don"t quite understand why you won"t report an error in this way. What does it mean to write like this?

let fn = () => {a: 1}
fn()

that"s why a: 1 won"t report an error. What does it mean? Solve ~ crab!

Apr.03,2021

ES6 arrow function: http://es6.ruanyifeng.com/-sharpdo.

In the code

above, the original intention was to return an object {a: 1}, but because the engine thought the curly braces were a block of code, a: 1 was executed. At this point, a can be interpreted as the label of the statement, so the statement actually executed is 1 position, and then the function ends with no return value.


Let me translate it for you


:

let fn = () => ({a: 1})
fn()

because if you don't add parentheses, the curly braces are the scope symbol of the method body rather than the literal quantity of the object. It must be wrong for you to put a a: 1 directly in the method body. Just add parentheses

.
Menu