I have an object of type ServiceWorkerRegistration
that I want to stringify:
const reg = await navigator.serviceWorker.register(`https://www.myurl.com/Worker.js`, {scope:"/"});
When I console.log(reg)
I get the following:
ServiceWorkerRegistration { installing: null, waiting: null, active: ServiceWorker, navigationPreload: NavigationPreloadManager, scope: "https://www.myurl.com/", updateViaCache: "imports", onupdatefound: null, pushManager: PushManager }
active: ServiceWorker { scriptURL: "https://www.myurl.com/Worker.js", state: "activated", onstatechange: null, … }
onerror: null
onstatechange: null
scriptURL: "https://www.myurl.com/Worker.js"
state: "activated"
<prototype>: ServiceWorkerPrototype { postMessage: postMessage(), scriptURL: Getter, state: Getter, … }
installing: null
navigationPreload: NavigationPreloadManager { }
onupdatefound: null
pushManager: PushManager { }
scope: "https://www.myurl.com/"
updateViaCache: "imports"
waiting: null
<prototype>: ServiceWorkerRegistrationPrototype { update: update(), unregister: unregister(), showNotification: showNotification(), … }
When I do typeof reg
I get 'object'
However if I try to JSON.stringify(reg)
I get {}
. Likewise if I try and do Object.keys(reg)
I get []
.
I looked at answers such as Why does JSON.stringify return empty object notation "{}" for an object that seems to have properties? and Why does JSON.stringify on TypeError return an empty object which claims this happens when an object has no enumerable properties.
A band aid solution is that I manually print each field e.g. console.log(reg["installing"]);
, or to manually reset each property as enumerable e.g. something like Object.defineProperty(reg, 'installing', {enumerable: true})
for each property.
However I would like to be able to do something like:
Object.keys(reg).forEach(key=>console.log(reg[key]));
And to do so, the object must have enumerable properties. How do I make the properties enumerable?