I'm seeking an elegant way to memoize a class function using Memoizee package.
Outside a class, you can go about this trivially:
const memoize = require('memoizee')
const myFunc = memoize(function myfunc(){ ... })
but inside a class block, this won't work:
class foo {
constructor(){ ... }
// Without memoization you would do:
myFunc(){ ... }
// Can't do this here:
myFunc = memoize(function myfunc(){ ... })
}
I can think of creating it in the constructor using this.
syntax, but this will result in a less uniform class definition, as non-memoized methods will be declared outside the constructor:
class foo {
constructor(){
// Inside for memoized:
this.myFunc = memoize(function myfunc(){ ... })
}
// Outside for non-memoized:
otherFunc(){ ... }
}
How would you wrap an instance method?
It's possible to overwrite own method definition in the constructor