* feat(tools): add seed/solution restore script * chore(curriculum): remove empty sections' markers * chore(curriculum): add seed + solution to Chinese * chore: remove old formatter * fix: update getChallenges parse translated challenges separately, without reference to the source * chore(curriculum): add dashedName to English * chore(curriculum): add dashedName to Chinese * refactor: remove unused challenge property 'name' * fix: relax dashedName requirement * fix: stray tag Remove stray `pre` tag from challenge file. Signed-off-by: nhcarrigan <nhcarrigan@gmail.com> Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
3.2 KiB
3.2 KiB
id, title, challengeType, forumTopicId, dashedName
id | title | challengeType | forumTopicId | dashedName |
---|---|---|---|---|
5a24c314108439a4d403615b | 使用 Object.assign 拷贝对象 | 6 | 301437 | copy-an-object-with-object-assign |
--description--
最后几个挑战适用于数组,但是当状态是object
时,有一些方法可以帮助强制执行状态不变性。处理对象的一个方法是Object.assign()
。Object.assign()
获取目标对象和源对象,并将源对象中的属性映射到目标对象。任何匹配的属性都会被源对象中的属性覆盖。通常用于通过传递一个空对象作为第一个参数,然后是要用复制的对象来制作对象的浅表副本。这是一个例子:
const newObject = Object.assign({}, obj1, obj2);
这会创建newObject
作为新的object
,其中包含obj1
和obj2
中当前存在的属性。
--instructions--
Redux 状态和 action 被修改为处理state
的对象
。编辑代码以返回一个新的state
对象,用于类型的 actionONLINE
,它将status
属性设置为字符串online
。尝试使用Object.assign()
来完成挑战。
--hints--
Redux store 应该存在并使用与第 1 行声明的defaultState
对象相同的状态进行初始化。
assert(
(function () {
const expectedState = {
user: 'CamperBot',
status: 'offline',
friends: '732,982',
community: 'freeCodeCamp'
};
const initialState = store.getState();
return DeepEqual(expectedState, initialState);
})()
);
wakeUp
和immutableReducer
都应该是函数。
assert(typeof wakeUp === 'function' && typeof immutableReducer === 'function');
dispatch 一个类型为ONLINE
的 action 应该将状态status
更新为online
,并且不应该改变状态。
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);
})()
);
Object.assign
应该被用于返回一个新状态。
(getUserInput) => assert(getUserInput('index').includes('Object.assign'));
--seed--
--seed-contents--
const defaultState = {
user: 'CamperBot',
status: 'offline',
friends: '732,982',
community: 'freeCodeCamp'
};
const immutableReducer = (state = defaultState, action) => {
switch(action.type) {
case 'ONLINE':
// Don't mutate state here or the tests will fail
return
default:
return state;
}
};
const wakeUp = () => {
return {
type: 'ONLINE'
}
};
const store = Redux.createStore(immutableReducer);
--solutions--
const defaultState = {
user: 'CamperBot',
status: 'offline',
friends: '732,982',
community: 'freeCodeCamp'
};
const immutableReducer = (state = defaultState, action) => {
switch(action.type) {
case 'ONLINE':
return Object.assign({}, state, {
status: 'online'
});
default:
return state;
}
};
const wakeUp = () => {
return {
type: 'ONLINE'
}
};
const store = Redux.createStore(immutableReducer);