0

Is there a way to override square-bracket accessor on a class, i.e.:

class Foo {

  constructor() {
    this.bar = {}
  }

  [prop]() {
    return this.bar[prop]
  }

}
Adam B.
  • 788
  • 5
  • 14
  • Have a look at [Proxy](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy) – Moritz Ringler Feb 07 '23 at 22:13
  • 1
    In particular `get` - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy/Proxy/get is what I believe you are looking for. – Alexei Levenkov Feb 07 '23 at 22:16
  • 1
    You can definitely create a getter function, but it will not be able to tell the difference between a `.` property access and a `[ ]` property access in all cases. – Pointy Feb 07 '23 at 22:16

0 Answers0