将方法移出类

时间:2019-05-30 03:49:40

标签: javascript

我纯粹出于学术原因想重构Cache类。

但是,我很难确定如何将getMapCache类中移出并将其作为常规函数使用。

function isObject(arg) {
  const typeOfObj = typeof arg;
  return (typeOfObj === 'object' || typeOfObj === 'function') && arg !== null;
}

class Cache {
  constructor() {
    this.map = new Map();
    this.weakmap = new WeakMap();
  }
  // return a Cache's value at a key
  getMap(key) {
    // console.log(this);
    const map = this[isObject(key) ? 'weakmap' : 'map'];
    // console.log(map);
    this.setKeyIfNeeded(map, key);
    let valueMap = map.get(key);
    return valueMap;
  }
  // create a Cache's key, if needed

  setKeyIfNeeded(map, key) {
    if (!map.has(key)) {
      map.set(key, new Cache());
    }
  }
}

const getNestedMap = (keys, initialCache) =>
  keys.reduce((cache, key) => cache.getMap(key), initialCache);

function memoize(fn) {
  const cache = new Cache();
  return (...args) => {
    // get (or create) a cache item

    const item = getNestedMap(args, cache);

    if (Reflect.has(item, 'value')) {
      return item.value;
    }
    return (item.value = fn(args));
  };
}

let counter = 1;
function foo() {
  counter += 1;
  return counter;
}

const id1 = Symbol('id');
const id2 = Symbol('id');

const memoizedFoo = memoize(foo);

console.log(memoizedFoo(3, 4, 5, 6)); //2
console.log(memoizedFoo(3, 4, 5, 6)); //2
console.log(memoizedFoo(3, 4, 6)); //3
console.log(memoizedFoo(3, 4, 6)); //3

2 个答案:

答案 0 :(得分:0)

您将需要重写您的函数以接受所有事物。然后,您只需在班级内调用它即可。

一个例子可能是:

getMap(cache, key, isObject, setKeyIfNeeded) {
    // console.log(this);
    const map = cache[isObject(key) ? 'weakmap' : 'map'];
    // console.log(map);
    setKeyIfNeeded(map, key);
    let valueMap = map.get(key);
    return valueMap;
  }

答案 1 :(得分:0)

解决方案

function isObject(arg) {
  const typeOfObj = typeof arg;
  return (typeOfObj === 'object' || typeOfObj === 'function') && arg !== null;
}

class Cache {
  constructor() {
    this.map = new Map();
    this.weakmap = new WeakMap();
  }

  static setKey(key, map) {
    return map.set(key, new Cache());
  }
}

function getCache(args, cache) {
  for (const key of args) {
    const map = cache[isObject(key) ? 'weakmap' : 'map'];
    cache = map.get(key) || Cache.setKey(key, map).get(key);
  }
  return cache;
}

function memoize(fn) {
  const cache = new Cache();
  return (...args) => {
    const item = getCache(args, cache);
    if (Reflect.has(item, 'value')) {
      return item.value;
    }
    return (item.value = fn(args));
  };
}

let counter = 1;
function foo() {
  counter += 1;
  return counter;
}