码迷,mamicode.com
首页 > 其他好文 > 详细

对reducers 理解

时间:2018-01-01 11:37:49      阅读:142      评论:0      收藏:0      [点我收藏+]

标签:bsp   匿名   item   post   man   计算   ali   color   console   

    var reducers = {  
      totalInEuros : function(state, item) {
        return state.euros += item.price * 0.897424392;
      },
      totalInYen : function(state, item) {
        return state.yens += item.price * 113.852;
      }
    };

    // reducers 是上面reducers 对象
    var manageReducers = function(reducers) {
      // return 是下面 items.reduce 匿名函数
      return function(state, item) {
          console.log(state, item); // state:{euros: 0} item:{price: 10}
        return Object.keys(reducers).reduce(
          function(nextState, key) {
              // 此处为state 进行计算后再返回
            reducers[key](state, item);
            return state;
          },
          {}
        );
      }
    };

    var bigTotalPriceReducer = manageReducers(reducers);

    var initialState = {euros:0, yens: 0};

    var items = [{price: 10}, {price: 120}, {price: 1000}];

    var totals = items.reduce(bigTotalPriceReducer, initialState);

    console.log(totals);

 

对reducers 理解

标签:bsp   匿名   item   post   man   计算   ali   color   console   

原文地址:https://www.cnblogs.com/y896926473/p/8166108.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!