I am curious how JavaScript EventSource and other similar objects having number and string properties can be converted to JSON.
I have tried some stuff with JSON.stringify(...)
and eventSource.entries()
but I get empty JSON.
I know that I can simply console.log(eventSource)
.
I am curious if I can make it a JSON not using console.log(...)
.
2
Answers
My best attempt so far has been to use
JSON.stringify(...)
putting my own newly created object created from object properties of an EventSource object I can see inconsole.log(eventSoure)
output.I doubt if it is of any practical value.
Just wanted to see it updated on the page and not in browser console.
Of course, properties CONNECTING, OPEN and CLOSE are just a set of possible values of readyState property that are added solely to see them on the page as well.
You can extract all properties with walking the prototype chain and collecting properties you want.
Use
Object.getOwnPropertyNames()
for that.For example I collect all primitive properties here: