Jim*_*Jim 3 javascript ecmascript-6 arrow-functions
例如在 redux 操作中,我在某人的代码中看到过:
export const updateMessage = text => {
return (dispatch) => {
dispatch(updateChatMessage(text))
}
}
Run Code Online (Sandbox Code Playgroud)
和:
const updateChatMessage = text => ({
type: types.someActionType,
text
})
Run Code Online (Sandbox Code Playgroud)
它似乎起到了暗示的作用 return但我认为这已经暗示在胖箭头后面的箭头函数括号中。
括号有({...})什么作用?他们有必要吗?有没有其他方法可以完成同样的事情?
当你写myFunction = value => ({prop: value})
它时返回对象{prop: value},在这种情况下{}是对象分隔符而不是“函数分隔符”
const updateChatMessage = text => ({
type: types.someActionType,
text
})
Run Code Online (Sandbox Code Playgroud)
另一个例如:
当您想将数组的每个元素乘以 2 时,您可以编写:
array.map(elem => {return elem * 2})
或者
array.map(elem => elem * 2) //同样的结果
如果你想要一个带有()包装对象的 eg :
const updateChatMessage = text => ({
type: types.someActionType,
text
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
597 次 |
| 最近记录: |