标签: memoizee

节点类中的 Memoizee 实例方法

我正在寻找一种优雅的方式来使用Memoizee 包记忆类函数。

在课堂之外,您可以轻松地进行以下操作:

const memoize = require('memoizee')

const myFunc = memoize(function myfunc(){ ... })
Run Code Online (Sandbox Code Playgroud)

但是在类块中,这不起作用:

class foo {
    constructor(){ ... }

    // Without memoization you would do:
    myFunc(){ ... }

    // Can't do this here:
    myFunc = memoize(function myfunc(){ ... })
}
Run Code Online (Sandbox Code Playgroud)

我可以考虑使用this.语法在构造函数中创建它,但这将导致类定义不太统一,因为非记忆方法将在构造函数之外声明:

class foo {
    constructor(){
        // Inside for memoized:
        this.myFunc = memoize(function myfunc(){ ... }) 
    }

    // Outside for non-memoized:
    otherFunc(){ ... }
}
Run Code Online (Sandbox Code Playgroud)

你将如何包装一个实例方法?

javascript class memoization node.js memoizee

3
推荐指数
1
解决办法
2186
查看次数

标签 统计

class ×1

javascript ×1

memoization ×1

memoizee ×1

node.js ×1