I would like to JSON.stringify
all properties of an object, including those defined via getters. However, when I call JSON.stringify
on an object, properties defined via getters are omitted:
> const obj = {key: 'val'}
undefined
> JSON.stringify(obj)
'{"key":"val"}'
> Object.defineProperty(obj, 'getter', {get: () => 'from getter'})
{ key: 'val' }
> obj.getter
'from getter'
> JSON.stringify(obj)
'{"key":"val"}'
I was hoping to see:
> JSON.stringify(obj)
'{"key":"val", "getter": "from getter"}'
Is this possible? Object.keys
isn't detecting the getters either:
> Object.keys(obj)
[ 'key' ]
Can you query for getter keys? Or do you have to know their names ahead of time?