object
时,有一些方法可以帮助强制执行状态不变性。处理对象的有用工具是Object.assign()
实用程序。 Object.assign()
接受目标对象和源对象,并将源对象中的属性映射到目标对象。任何匹配的属性都会被源对象中的属性覆盖。此行为通常用于通过将空对象作为第一个参数传递,然后传递要复制的对象来生成对象的浅副本。这是一个例子: const newObject = Object.assign({}, obj1, obj2);
这将newObject
创建为一个新object
,其中包含当前存在于obj1
和obj2
中的属性。 state
的object
。编辑代码以返回类型为ONLINE
操作的新state
对象,该status
属性将status
属性设置为online
字符串。尝试使用Object.assign()
来完成挑战。 defaultState
对象的状态进行初始化。
testString: 'assert((function() { const expectedState = { user: ''CamperBot'', status: ''offline'', friends: ''732,982'', community: ''freeCodeCamp'' }; const initialState = store.getState(); return DeepEqual(expectedState, initialState); })());'
- text: wakeUp
和immutableReducer
都应该是函数。
testString: assert(typeof wakeUp === 'function' && typeof immutableReducer === 'function');
- text: 调度ONLINE
类型的操作应该将status
中的属性status
更新为online
并且不应该改变状态。
testString: 'assert((function() { const initialState = store.getState(); const isFrozen = DeepFreeze(initialState); store.dispatch({type: ''ONLINE''}); const finalState = store.getState(); const expectedState = { user: ''CamperBot'', status: ''online'', friends: ''732,982'', community: ''freeCodeCamp'' }; return isFrozen && DeepEqual(finalState, expectedState); })());'
- text: Object.assign
应该用于返回新状态。
testString: getUserInput => assert(getUserInput('index').includes('Object.assign'));
```