繁体   English   中英

将参数带入另一个函数

[英]taking argument in to another function

一个参数-另一个函数-并返回该函数的“记忆”版本。 函数的“内存化”版本会缓存并返回其调用的结果,以便在再次使用相同的输入再次调用该函数时,它不会运行其计算,而是从缓存中返回结果。 请注意,以前的结果应可以任何顺序检索,而无需重新计算。

foo = function (x) {
console.log("calculating!");
return x + 5;
}

var memoizedFoo = memoize(foo);

memoizedFoo(5);
// calculating!
// 10

memoizedFoo(5);
// 10 (notice how 'calculating!' is not printed this time)

memoizedFoo(10);
// calculating!
// 15

我认为问题是如何写memoize 您可以将给定参数的第一个结果存储在容器中,并返回一个将使用该容器的函数。

这是一个ES5示例,仅适用于可有效转换为字符串(例如字符串,数字,布尔值)的参数值:

 function memoize(f) { // Storage for this function's results var values = Object.create(null); return function(arg) { // Already have it? if (Object.hasOwnProperty.call(values, arg)) { // Yes, return it return values[arg]; } // No, get it, remember it, and return it return values[arg] = f(arg); }; } var foo = function (x) { console.log("calculating!"); return x + 5; }; var memoizedFoo = memoize(foo); console.log(memoizedFoo(5)); // calculating! // 10 console.log(memoizedFoo(5)); // 10 console.log(memoizedFoo(10)); // calculating! // 15 

如果需要支持其他类型的参数,则需要使用其他结构或Map polyfill。 这带我们去...

...在ES2015 +中,我们可以使用Map ,这使得它可以与更广泛的参数值一起使用:

 function memoize(f) { // Storage for this function's results const values = new Map(); return function(arg) { // Already have it? if (values.has(arg)) { // Yes, return it return values.get(arg); } // No, get it, remember it, and return it const value = f(arg); values.set(arg, value); return value; }; } const foo = function (x) { console.log("calculating!"); return x.foo + 5; }; const memoizedFoo = memoize(foo); const firstArg = {foo:5}; console.log(memoizedFoo(firstArg)); // calculating! // 10 console.log(memoizedFoo(firstArg)); // 10 const secondArg = {foo:10}; console.log(memoizedFoo(secondArg)); // calculating! // 15 // Note that maps consider equivalent but distinct objects as different (of course), // so we don't get the memoized reslt from // `firstArg` here const thirdArg = {foo:5}; console.log(memoizedFoo(secondArg)); // calculating! // 10 

暂无
暂无

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

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