function createStore(reducer,initialState){ let state = initialState || undefined let listener = [] function getState(){ return state } function subscribe(cb){ listener.push(cb) return function(){ let index = listener.indexOf(cb) listener.splice(index,1) } } function dispatch(action){ state = reducer(state,action) listener.forEach((cb)=>{ cb() }) return action } dispatch({type:'@redux'}) return { getState, subscribe, dispatch } } function reducer(state,action){ switch(action.type){ case '1' : return 'bai' case '2' : return 'ming' default : return state || '^^__^^' } } let store = createStore(reducer,25) console.log(store.getState()) function render(){ console.log(store.getState()) } let unsubscribe = store.subscribe(render) console.log(store.dispatch({type:'1'})) unsubscribe() console.log(store.dispatch({type:2}))