Javascript 将方法移出类
纯粹出于学术原因,我想重新考虑缓存类 但是,我很难弄清楚如何将Javascript 将方法移出类,javascript,Javascript,纯粹出于学术原因,我想重新考虑缓存类 但是,我很难弄清楚如何将getMap移出Cache类并将其作为常规函数 function isObject(arg) { const typeOfObj = typeof arg; return (typeOfObj === 'object' || typeOfObj === 'function') && arg !== null; } class Cache { constructor() { this.map = n
getMap
移出Cache
类并将其作为常规函数
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
您需要重写函数以接受所有内容。那你就从你班里叫它 一个例子可能是:
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;
}
您需要重写函数以接受所有内容。那你就从你班里叫它 一个例子可能是:
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;
}
解决方案
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;
}
解决方案
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;
}
const getMap=>(key)=>{…}
应该工作吗?我想知道您是否想让代码在没有缓存的情况下工作。只是我不明白为什么你只想把代码> > GETMAP < /代码>出类,所以@ YasIkeda值得考虑作为一个想法来实现<代码> const GETMAP= >(key)={…} <代码>,我应该想知道你是否意味着你想让你的代码在没有<代码> Cache < /Cord>类的情况下工作。只是我不明白为什么你只想把代码> > GETMAP < /代码>出类,所以@ YasIkeda值得考虑作为一个想法来实现。