JavaScript中的回忆功能

发布于 2025-01-22 11:10:41 字数 738 浏览 0 评论 0原文

因此,当我试图了解纪念的真正起作用时,我遇到了一个Momoize功能,解决方案确实让我思考下面的代码

const memoize = (fn) => {
  const cache = {};
  return (...args) => {
    let cacheKey = args.map(n => n.toString() + '+').join('');
    if (cacheKey in cache) {
      console.log(cache[cacheKey])
      return cache[cacheKey];
    }else {
      let result = args.reduce((acc, curr) => fn(acc, curr), 0);
      cache[cacheKey] = result;
      console.log(result)
      return result;
    }
  }
}
const add = (a, b) => a + b;
const memoizeAdd = memoize(add)
memoizeAdd(1, 2, 3, 4)

我的问题是,如果添加函数仅接受2个参数,则MOMOIZE变量如何将添加函数作为参数作为参数,而Memoizeadd也需要广泛的参数?拜托,这个问题来自一个好奇的地方。

So I came across a momoize function when I was trying to understand how memoization really works, the solution has really had me thinking, the code below

const memoize = (fn) => {
  const cache = {};
  return (...args) => {
    let cacheKey = args.map(n => n.toString() + '+').join('');
    if (cacheKey in cache) {
      console.log(cache[cacheKey])
      return cache[cacheKey];
    }else {
      let result = args.reduce((acc, curr) => fn(acc, curr), 0);
      cache[cacheKey] = result;
      console.log(result)
      return result;
    }
  }
}
const add = (a, b) => a + b;
const memoizeAdd = memoize(add)
memoizeAdd(1, 2, 3, 4)

My question is how the momoize variable takes the add function as an argument and memoizeAdd also takes a wide range of argument, If add function only accepts 2 arguments? please, this question comes from a place of curiosity.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鸠书 2025-01-29 11:10:41

add作为参数传递给remoize函数。如果仔细观察,您会注意到fn(指的是add)始终仅使用两个参数调用。

这是因为Memoize函数与fn参数一起调用redaim(即使没有定义的长度,也旨在处理数组)。

有效地,使用只需添加两个参数的函数应用REDAL将返回所有元素的总和,这是memoizeadd的预期结果。

我认为检查如何减少工作 可能会帮助您

add is passed as the argument to the memoize function. If you look closely, you will notice that fn (which is referring to add) is always called with two arguments only.

This is because the memoize function calls the fn argument along with reduce (which is meant to process an array, even without a defined length).

Effectively, applying reduce with a function that just adds two parameters will return the sum of all the elements, which is the expected result of memoizeAdd.

I think checking how reduce works might help you

风吹雪碎 2025-01-29 11:10:41

有两件事。

  1. 我们可以通过函数作为参数在javascript中(并且可以从Javascript中执行(并且可以从那里)。
  2. 而您创建的是 closure

闭合是捆绑在一起的函数的组合(封闭)
提到其周围状态(词汇环境)。

当您创建和分配Memoizeadd时,CLOSURE会为添加功能创建其范围。并返回另一个获得许多(... args)参数的函数。

现在,您将该返回的方法(memoizeadd)带有参数。

变量chache将用于此add方法范围。

现在,如果您现在创建另一个记忆,

const mul = (a, b) => a + b;
const memoizeMul = memoize(add)
memoizeMul(1, 2, 3, 4)

它将创建另一个范围,并将高速缓存与add版本分开。

Two things are there.

  1. We can pass function as parameter in JavaScript(and can be executed from there).
  2. And what you have been created is a closure

A closure is the combination of a function bundled together (enclosed)
with references to its surrounding state (the lexical environment).

When you created and assigned memoizeAdd, closure creates its scope for add function. and returns another function that takes many(...args) arguments.

now you called that returned method(memoizeAdd) with arguments.

the variable chache will be available for this add method scope.

Now if you creates another memoize

const mul = (a, b) => a + b;
const memoizeMul = memoize(add)
memoizeMul(1, 2, 3, 4)

Now it creates another scope and keep cache separate from the add version.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文