0

I want to define some class or object where I can do

Foo['bar']

This get item call should call a function so that I can provide a default or some other logic based on the key. Similar to python's __getitem__

const Foo = {
   __getitem__: key => {
     if (key in Foo.values) return Foo.values[key]
     // other logic
     return key
   }

}
david_adler
  • 9,690
  • 6
  • 57
  • 97
  • 1
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/get should help you – Benjamin Eckardt Jun 30 '20 at 10:19
  • 1
    You will need a [`Proxy`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Proxy) for this -> https://stackoverflow.com/a/29723887/402037 – Andreas Jun 30 '20 at 10:22

0 Answers0