2016-07-23 119 views
2

什么是“let 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); 

来源:https://gist.github.com/btroncone/a6e4347326749f938510

+1

这只是一堆作为参数传递的函数。我认为,顶层函数在某个时候被称为缩减器映射。 –

+0

你能发送一个链接来解释这个或更好地解释你的意思吗? –

+0

具体解释一下?箭头功能在ES2015文档/教程中进行了解释。 'reduce'具有正常的功能文档。你在问Redux部分吗? –

回答

1

设X = something1 => something2 => something3是如下面的几乎相同:

let x = function (something) { 
 
    return function (something2) { 
 
    return something3 
 
    } 
 
}

唯一的区别是,箭头具有this词法结合,即,在编译结合时间。

+0

在进程结束时x = something3吗? –

+1

编号'x'现在是一个函数表达式。如果你调用let abc = x()。然后abc将我们等于东西3 –

+0

好吧,我明白了。谢谢你的回复! –

3

箭头功能

someParameters => someExpression 

那么,什么是

someParameters => someThing => someThingElse 

???

好,通过简单的傻瓜 “模式匹配”,这是一个箭头的功能,它的机身(someExpression)是

someThing => someThingElse 

换句话说,它是

someParameters => someOtherParameters => someExpression 

没有什么特别之处。函数是对象,它们可以从函数返回,而不管这些函数是使用箭头还是关键字function写入的。

你真的需要知道读这正确的是,箭头是右结合的唯一的事情,督察该

a => b => c === a => (b => c) 

注:箭头的功能也可以由语句的身体以及一个单一的表达。我具体指的是OP令人困惑的形式。

+0

感谢您的时间和很好的回答,但我选择了另一个,因为某些原因,我对此更加清楚。谢谢你。 –

+0

没关系,我的答案假设人们已经知道什么是箭头函数,Aakash的解释是没有箭头函数。 –