对javascript的疑惑适用-函数记忆 [英] doubts on javascript apply - function memoization

查看:56
本文介绍了对javascript的疑惑适用-函数记忆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力寻找一本书中的js备忘录示例,这是代码:

I'm struggling with an example of js memoization found on a book, here's the code:

Function.prototype.memoized = function(key){
    this._values = this._values || {};
    return this._values[key] !== undefined ? this._values[key] : this._values[key] = this.apply(this, arguments);
}

这是带有完整示例的 小提琴

here's a fiddle with a complete example

我真正不了解的是这段代码的工作方式及其作用,特别是apply部分:

what I don't really get is how this piece of code works and what it does, in particular the apply part:

return this._values[key] !== undefined ? this._values[key] : this._values[key] = this.apply(this, arguments);

我知道并了解apply的工作方式

I know and understand how apply works

apply()方法调用具有给定值和以数组形式提供的参数的函数

The apply() method calls a function with a given this value and arguments provided as an array

假设this._values[key]等于undefined,则返回的值将是this.apply(this, arguments):此代码是否重新启动了memoized函数?我试图在该函数内添加一些日志,以查看该函数被调用了多少次,但似乎只启动了一次..

suppose that this._values[key] is equal to undefined, then the returned value will be this.apply(this, arguments): does this code re-launch the memoized function? I've tried to add some logs inside the function to see how many times the function is called, but it seems it's been launched only once..

有人可以给我一个提示吗?这可能是一个虚拟的问题,请耐心等待,谢谢

Can anyone please give me a hint? It's probably a dummy question, please be patient, thanks

推荐答案

让我们使用一个简单的示例斐波那契数字.

Let's use a simple example, fibonacci numbers.

function fib(n) {
    if (n < 2) return 1;
    return fib.memoized(n-1) + fib.memoized(n-2);
}

在这里我们可以看到memoized方法已应用于fib函数,即您的this设置为函数本身的情况下调用它,这没有任何意义.更好:

Here we can see that the memoized method is applied on the fib function, i.e. your this keyword refers to the fib function. It does not relaunch the memoized function, but "launches" the function on which it was called. However, it does call it with this set to the function itself, which does not make any sense. Better:

Function.prototype.memoize = function(key){
    if (!this._values)
        this._values = {};
    if (key in this._values)
        return this._values[key];
    else
        return this._values[key] = this.apply(null, arguments);
// pass null here:                            ^^^^
}

更好的是,如果memoized返回一个闭包:

Even better would be if memoized would return a closure:

Function.prototype.memoized = function(v) {
    var fn = this, // the function on which "memoized" was called
        values = v || {};
    return function(key) {
        if (key in values)
            return values[key];
        else
            return values[key] = fn.apply(this, arguments);
    }
}
var fib = function(n) {
    if (n < 2) return 1;
    return fib(n-1) + fib(n-2);
}.memoized();
// or even
var fib = function(n) { return fib(n-1) + fib(n-2) }.memoized({0:1, 1:1});

这篇关于对javascript的疑惑适用-函数记忆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆