您的位置:首页 > 移动开发 > Objective-C

[Redux] Avoiding Object Mutations with Object.assign() and ...spread

2015-11-30 03:31 423 查看
Learn how to use Object.assign() and the spread operator proposed for ES7 to avoid mutating objects.

/*
* Open the console to see
* that the tests have passed.
*/

const toggleTodo = (todo) => {
return {
...todo,
completed: !todo.completed
};
};

const testToggleTodo = () => {
const todoBefore = {
id: 0,
text: 'Learn Redux',
completed: false
};
const todoAfter = {
id: 0,
text: 'Learn Redux',
completed: true
};

deepFreeze(todoBefore);

expect(
toggleTodo(todoBefore)
).toEqual(todoAfter);
};

testToggleTodo();
console.log('All tests passed.');


Here '...todo', spread object, whcih is not available in ES6, but will be in ES7, It is fairly popular, and it is enabled in Babel if you use the stage two preset.

https://babeljs.io/docs/plugins/preset-stage-2/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: