Search code examples
javascriptecmascript-6getter-settergetter

javascript and memoized getters


This article describe getters. It has a section " Smart / self-overwriting / lazy getters" And it's unclear for me, are getters 'memoized' by default or should I implement this feature by myself

e.g.

class Foo() {
  get boo() {
    this._boo = this._boo || new Boo(); 
    return this._boo;  
  }
}

or can I just write:

class Foo() {
  get boo() {
    return new Boo();  
  }
}

to have the same result?


Solution

  • No, there is no language-level support for memoized getters in JavaScript. In your second example, a new object would be created every time boo was accessed.