I’m creating a dictionary object in javascript (eg associate array)
var myobj = {
"a" : "Some string",
"b" : "Some string else"
}
if I access myobj.a, it will return “Some string”
but if I access something not in the list myobj.c it return undefined.
Is there any way to set object getter to return something like: “Key not available”
Can we use prototype…
Thanks for any help
Yes/No.
No
There is no way to intercept generic get’s on keys using ES5
Yes
Use methods
Note there are some pitfalls using objects as dictionary, consider using a module like strmap
Use ES6 proxies (not implemented in browsers)
Using an ES6 proxy
Using an ES6 Map (shimmable)
Just use an ES6 map