const a = "hello"
const b = () => ({a})
这仅是该等效粗箭头功能的简写:
const b = () => { return {a} }
或者它还有其他用途吗?你怎么称呼这些?似乎只是为了保存一个保留字而添加结构似乎很多。
更简洁,有一条特殊的规则,即如果右侧是要返回的单个表达式,则不必是{ return expr }
,而只需是expr
。
const sq = (x => x * x);
const hyphenAround = (s => `-${s}-`);
[1, 3, 5].map(a => a * a)
[1, 3, 5].reduce((a, b) => a + b)
const sq = (x => x * x); const hyphenAround = (s => `-${s}-`); console.log(sq(3)); console.log(hyphenAround("hello")); console.log([1, 3, 5].map(a => a * a)); console.log([1, 3, 5].reduce((a, b) => a + b));
在您的示例中是
const a = "hello" const b = () => ({a})
与]相同>
const a = "hello" const b = () => ({a: a})
那些被称为shorthand property names。let a = "hello" const b = () => ({ a }); console.log(b()); a = "a long sentence"; console.log(b()); x = 123; y = "hello" z = [1, 3, 5]; console.log({ x, y, z });