简体   繁体   English

在 javascript 中记忆 function

[英]memoization function in javascript

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所以当我试图了解记忆化的真正工作原理时,我遇到了一个 momoize function,这个解决方案让我思考,下面的代码

 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?我的问题是 momoize 变量如何将 add function 作为参数并且 memoizeAdd 也采用广泛的参数,如果 add function 只接受 2 arguments? please, this question comes from a place of curiosity.拜托,这个问题来自一个好奇的地方。

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. add作为参数传递给memoize function。如果仔细观察,您会注意到fn (指的是add )总是只用两个 arguments 调用。

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).这是因为memoize function 调用fn参数和reduce (这意味着处理一个数组,即使没有定义的长度)。

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 .实际上,使用仅添加两个参数的 function 应用reduce将返回所有元素的总和,这是memoizeAdd的预期结果。

I think checking how reduce works might help you我认为检查reduce 的工作原理可能会对您有所帮助

Two things are there.有两件事。

  1. We can pass function as parameter in JavaScript(and can be executed from there).我们可以在 JavaScript 中将 function 作为参数传递(并且可以从那里执行)。
  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).闭包是将 function 捆绑在一起(封闭)并引用其周围的 state(词法环境)的组合。

When you created and assigned memoizeAdd, closure creates its scope for add function. and returns another function that takes many(...args) arguments.当您创建并分配 memoizeAdd 时,闭包会为添加 function 创建其 scope。并返回另一个 function,它需要许多(...args)arguments。

now you called that returned method(memoizeAdd) with arguments.现在你用 arguments 调用返回的方法(memoizeAdd)。

the variable chache will be available for this add method scope.变量chache将可用于此add方法 scope。

Now if you creates another memoize现在,如果你创建另一个 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.现在它创建另一个 scope 并将缓存与add版本分开。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM