什么是"让x = something1 => something2 => something3" ?
我有这段代码,而我却无法理解它的作用。
const myReducers = {person, hoursWorked};
const combineReducers = reducers => (state = {}, action) => {
return Object.keys(reducers).reduce((nextState, key) => {
nextState[key] = reducers[key](state[key], action);
return nextState;
}, {});
};
您需要完整的代码:
//Redux-Style Reducer
const person = (state = {}, action) => {
switch(action.type){
case 'ADD_INFO':
return Object.assign({}, state, action.payload)
default:
return state;
}
}
const infoAction = {type: 'ADD_INFO', payload: {name: 'Brian', framework: 'Angular'}}
const anotherPersonInfo = person(undefined, infoAction);
console.log('***REDUX STYLE PERSON***: ', anotherPersonInfo);
//Add another reducer
const hoursWorked = (state = 0, action) => {
switch(action.type){
case 'ADD_HOUR':
return state + 1;
case 'SUBTRACT_HOUR':
return state - 1;
default:
return state;
}
}
//Combine Reducers Refresher
****HERE****
****HERE****
****HERE****
const myReducers = {person, hoursWorked};
const combineReducers = reducers => (state = {}, action) => {
return Object.keys(reducers).reduce((nextState, key) => {
nextState[key] = reducers[key](state[key], action);
return nextState;
}, {});
};
****
****
/*
This gets us most of the way there, but really want we want is for the value of firstState and secondState to accumulate
as actions are dispatched over time. Luckily, RxJS offers the perfect operator for this scenario., to be discussed in next lesson.
*/
const rootReducer = combineReducers(myReducers);
const firstState = rootReducer(undefined, {type: 'ADD_INFO', payload: {name: 'Brian'}});
const secondState = rootReducer({hoursWorked: 10, person: {name: 'Joe'}}, {type: 'ADD_HOUR'});
console.log('***FIRST STATE***:', firstState);
console.log('***SECOND STATE***:', secondState);
答案 0 :(得分:4)
箭头功能
someParameters => someExpression
那么,
是什么someParameters => someThing => someThingElse
???
嗯,通过简单的愚蠢"模式匹配",它是一个箭头函数,其正文(someExpression
)
someThing => someThingElse
换句话说,它是
someParameters => someOtherParameters => someExpression
这没什么特别的。函数是对象,无论函数是使用箭头还是function
关键字编写,都可以从函数返回。
你真正需要知道的唯一一件事就是箭头是右关联的,IOW
a => b => c === a => (b => c)
注意:箭头函数也可以包含由语句和单个表达式组成的正文。我特别指的是OP困惑的形式。
答案 1 :(得分:2)
让x = something1 => something2 => something3几乎与以下内容相同:
let x = function (something) {
return function (something2) {
return something3
}
}
唯一的区别是,箭头具有this
的词法绑定,即在编译时绑定。