Reputation: 4479
I'm exploring ES6 symbols, trying to understand what they are and I cannot wrap my head around this behavior
> let sym = Symbol()
> let a = {}
> a[sym]= "value"
'value'
> a
{}
> Object.keys(a)
[]
> Object.getOwnPropertyNames(a)
[]
> a[sym]
'value'
> a[Symbol()]
undefined
> JSON.stringify(sym)
undefined
> JSON.stringify(a)
'{}'
> sym
Symbol()
I'm not understanding symbols yet, but Where is the key and value hiding?!? Did java script fall into the realm of witchcraft and wizardry? Is there anyway to tell it is there at all?
EDIT::
Thank you for notifying me about Object.getOwnPropertySymbols();
So with ES6 I have to check Object.getOwnPropertyNames() and Object.getOwnPropertySymbols() for hidden non enumerable properties?
Upvotes: 0
Views: 87
Reputation: 17703
From MDN it looks like you can get them with Object.getOwnPropertySymbols(a)
.
I stumbled across this article which provides some additional context: http://blog.keithcirkel.co.uk/metaprogramming-in-es6-symbols/
Symbols give a whole new sense of purpose to Objects - they provide a kind of hidden under layer to Objects - not iterable over, not fetched using the already existing Reflection tools and guaranteed not to conflict with other properties in the object!
Upvotes: 4