在Java语言中,您怎么称呼这个“身份函数”-()=>({obj})[重复]

问题描述 投票:0回答:1
我已经看过这种JavaScript代码(人为设计):

const a = "hello" const b = () => ({a})

这仅是该等效粗箭头功能的简写:

const b = () => { return {a} }

或者它还有其他用途吗?你怎么称呼这些? 

似乎只是为了保存一个保留字而添加结构似乎很多。

javascript function syntax
1个回答
0
投票
我认为您正在询问箭头功能的右侧。

更简洁,有一条特殊的规则,即如果右侧是要返回的单个表达式,则不必是{ 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 });
© www.soinside.com 2019 - 2024. All rights reserved.