看看redux todomvc,为什么以下之间没有按键{}?例如之前text和id之后没有钥匙?有点困惑.
import * as types from '../constants/ActionTypes'
export const addTodo = text => ({ type: types.ADD_TODO, text })
export const deleteTodo = id => ({ type: types.DELETE_TODO, id })
export const editTodo = (id, text) => ({ type: types.EDIT_TODO, id, text })
export const completeTodo = id => ({ type: types.COMPLETE_TODO, id })
export const completeAll = () => ({ type: types.COMPLETE_ALL })
export const clearCompleted = () => ({ type: types.CLEAR_COMPLETED })
Run Code Online (Sandbox Code Playgroud)
欢迎任何评论.谢谢.
UPDATE
感谢您的回答和评论.我发现我的问题有点愚蠢.目前,我正在学习反应和减少乐趣.我对es2015不熟悉.有时,我无法区分2015es新语法和redux语法.谢谢.
在ES2015(和JSX)对象初始化器中,您可以通过仅提供变量名而不在变量后加上值来在对象上创建属性。该属性的名称将与该变量的名称相同,并且该属性的值将是该变量的值:
let one = 1;
let obj = {one};
console.log(obj.one); // 1Run Code Online (Sandbox Code Playgroud)
所以在这一行:
export const addTodo = text => ({ type: types.ADD_TODO, text })
Run Code Online (Sandbox Code Playgroud)
不是因为键(属性名称)不存在,而是属性的值(显式)不存在text。就像是这样写的:
export const addTodo = text => ({ type: types.ADD_TODO, text: text })
// ---------------------------------------------------------^^^^^^
Run Code Online (Sandbox Code Playgroud)
这只是简写。但是令人惊讶的是它有用的频率。
ES6引入了对象的简写.在早期的实现中,我们需要做这样的事情:
var id = "foo";
var obj = {
id: id
}
Run Code Online (Sandbox Code Playgroud)
但是使用ES6我们可以简单地这样做:
var id = "foo";
var obj = { id };
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
991 次 |
| 最近记录: |