2016-07-23 88 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的解釋是沒有箭頭函數。 –