Here, Reflect is being used to take care of the "default" case of accessing an array using a normal numerical index.
The rest of the time we're abusing the fact that any key in arr[key] form gets coerced into a string, so can be parsed into whatever we like once it reaches the getter/setter in a Proxy handler.
I posted this elsewhere, but here's an example use-case:
Let's say you want to use arrays as accessors, like this:
You can use
Proxy
andReflect
to achieve it:Here,
Reflect
is being used to take care of the "default" case of accessing an array using a normal numerical index.The rest of the time we're abusing the fact that any
key
inarr[key]
form gets coerced into a string, so can be parsed into whatever we like once it reaches the getter/setter in aProxy
handler.this looks very helpful