什么是双箭头函数?

Sta*_*lfi 10 javascript typescript arrow-functions

什么是“让 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;
  }, {});
};
Run Code Online (Sandbox Code Playgroud)

您需要的完整代码:

//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);
Run Code Online (Sandbox Code Playgroud)

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

Aak*_*del 9

let x= something1 => something2 => something3 is almost same as the following:

let x = function (something) {
  return function (something2) {
    return something3
  }
}
Run Code Online (Sandbox Code Playgroud)

The only difference is, arrows have lexical binding of this, i.e. binding in compile time.

  • 不。`x` 现在是一个函数表达式。如果您调用 let abc = x()。那么 abc 会等于 something3 (2认同)

Jör*_*tag 5

箭头函数是

someParameters => someExpression
Run Code Online (Sandbox Code Playgroud)

那么,什么是

someParameters => someThing => someThingElse
Run Code Online (Sandbox Code Playgroud)

???

好吧,通过简单愚蠢的“模式匹配”,它是一个箭头函数,其主体 ( someExpression) 是

someThing => someThingElse
Run Code Online (Sandbox Code Playgroud)

换句话说,它是

someParameters => someOtherParameters => someExpression
Run Code Online (Sandbox Code Playgroud)

这没有什么特别的。函数是对象,它们可以从函数返回,无论这些函数是使用箭头还是function关键字编写的。

要正确阅读本文,您真正需要知道的唯一一件事是箭头是右结合的,IOW

a => b => c === a => (b => c)
Run Code Online (Sandbox Code Playgroud)

Note: An arrow function can also have a body consisting of statements as well as a single expression. I was specifically referring to the form the OP is confused about.