2017.08.27

New

  • 构建一个可以深层不可变对象的工具函数
const Object.deepFreeze = function(obj) {
    // 获取对象的所有属性名
    var propNames = Object.getOwnPropertyNames(obj);
    // 遍历对象的所有属性,如果为对象则继续调用本函数,否则返回渐层锁函数即可
    propNames.forEach((name) => {
        if (typeof obj[name] == 'object' && obj[name] !== null) {
            Object.deepFreeze(obj[name]);
        }
    });
    return Object.freeze(obj);
}
  • Redux Middleware
const logger = store => next => action => {
  console.log('dispatching', action)
  let result = next(action)
  console.log('next state', store.getState())
  return result
};
const logger = function(store) {
    return function(next) {
        return function(action) {
            console.log('dispatch', action);
            let result = next(action);
            console.log('next state', store.getState())
        }
    }
}

results matching ""

    No results matching ""