2016-07-23 3 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 docs/tutorials에서 설명합니다. 'reduce'는 정상적인 함수를 가지고 있습니다. Redux 부분에 대해 묻고 있습니까? –

답변

1

하자 X = something1 => something2 =는> something3는 다음과 거의 같다 시각.

+0

x = something3 처리가 끝났습니까? –

+1

'x'는 이제 함수 표현식입니다. let abc = x()를 호출하면됩니다. 그러면 abc는 우리가 무언가와 같을 것입니다 3 –

+0

알겠습니다. 알겠습니다. 답장을 보내 주셔서 감사합니다! –

3

가 화살표의 기능은 그래서

someParameters => someExpression 

입니다

someParameters => someThing => someThingElse 
것입니다

???

음, 단순한 바보 "패턴 일치"함으로써, 본문 화살표 기능 (someExpression가) 즉

someThing => someThingElse 

입니다, 그것은 이것에 대해 특별한 거기에 아무것도

someParameters => someOtherParameters => someExpression 

입니다. 함수는 객체이므로 화살표 또는 function 키워드를 사용하여 함수를 작성했는지 여부에 관계없이 함수에서 반환 할 수 있습니다.

유일한 당신이 정말로이 제대로 화살표 오른쪽 연관이 있음을 읽기 위해 알아야 할 것은, IOW 그

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

주 : 화살표의 기능은 문으로 구성된 몸을 가질 수있다 단일 표현식뿐만 아니라 나는 OP가 혼란스러워하는 형태를 구체적으로 언급하고 있었다. ,

let x = function (something) { 
 
    return function (something2) { 
 
    return something3 
 
    } 
 
}
유일한 차이점은

가 화살표 this 어휘 결합을 컴파일에 결합하여 예 :

+0

당신의 시간과 훌륭한 답변을 주셔서 감사합니다. 그러나 나는 어떤 이유로 그것이 나에게 더 분명했기 때문에 다른 하나를 선택했습니다. 고마워. –

+0

그건 괜찮아, 내 대답은 이미 화살표 기능이 뭔지 알고 있다고 가정하고, Aakash는 화살표 기능이 없다고 설명한다. –

관련 문제