In ES6 , Arrow Function is introduced. it’s like this:
const temp = (param1, param2) => { let i = param1 + param2; return i};
when it comes to the lesson of “Write Concise Object Literal Declarations Using Object Property Shorthand” to return an object, it becomes:
const temp = (param1, param2) =>( {param1, param2});
Yes, I mean the parentheses appear to enclose the curly bracket. and makes the syntax hard to understand and remember. Why are these parenthese needed?
And this leads to another problem, the test requires me to only edit the code between the two comments. But it seems that I have to add a left parenthesis before the curly bracket after the arrow to creat a “person”, which means I have to change the code beyond the scope. Am I wrong? or anything i just missed?
If you do not have curly brackets in the arrow function it will execute the expression and return that value, the reason you have parentheses here is so you can immediately return an object other wise the arrow function would require you to do this instead:
Absolutely, so there are at least three forms behind the arrow as far as I’ve learned:
with curly brackets and some statements inside,
no bracket at all for simple expression,
with parenthese and curly brackets inside for returning object.
To some extent, I’m not in favor of such conciseness and diversity.
Many thanks!