xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

es6 curry function

es6 curry function


//  vuex getters

export const getAdsFilterConfig = (state) => (spreader) => {
    console.log('state =', state)
    console.log('spreader =', spreader)
    return state[spreader];
};

// this.getAdsFilterConfig(this.spreaderAlias);
// export const stateActivity = state => state.stateActivity;


equal to

// getter 自动执行,getter func ===  func()

export const getAdsFilterConfig = (state) => {
    // state 来自 store 上下文
    return (spreader) => {
        return state[spreader];
    };
}


https://codesandbox.io/s/vuex-store-getter-es6-curry-function-n7prr?file=/src/main.js

bug

state = {k: 1};
// {k: 1}

getAdsFilterConfig = (state) => (spreader) => {
    return state[spreader];
};
/*
(state) => (spreader) => {
    return state[spreader];
}
*/
getAdsFilterConfig(`k`);
/*
(spreader) => {
    return state[spreader];
}
*/

getAdsFilterConfig()(`k`);
// Uncaught TypeError: Cannot read property 'k' of undefined



vuex

const store = new Vuex.Store({
  state: {
    todos: [
      { id: 1, text: '...', done: true },
      { id: 2, text: '...', done: false }
    ]
  },
  getters: {
    doneTodos: state => {
      return state.todos.filter(todo => todo.done)
    }
  }
})

https://vuex.vuejs.org/guide/getters.html#method-style-access

refs



©xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


posted @ 2020-12-29 19:12  xgqfrms  阅读(95)  评论(4编辑  收藏  举报