javascript - Serializing Coordinates: Object.keys() fails on instances of native interfaces? -




i have javascript running in chrome using geolocation api:

navigator.geolocation.watchposition((pos) => {     console.log(pos.coords);     console.log(object.keys(pos.coords);    }, (err) => {     alert('geolocation error.\n' + err.message);    }, {     enablehighaccuracy: true   }); 

when chrome detects change in location, fires off first callback. working well.

the problem comes when try data. when callback fired, on console first able see coordinates object called console.log(pos.coords):

coordinates {latitude: 5.520007, longitude: 13.404954, altitude: null, accuracy: 150, altitudeaccuracy: null, …}

however, next line call console.log(object.keys(pos.coords)), empty array:

[]

likewise, if try use json.stringify(pos.coords), empty object {}.

is there reasonable way loop through keys of pos.coords? must make own list of keys loop through?

object.keys(pos.coords) gets empty array

yes, properties of coordinates interface implemented getters on prototype object (instead of own properties), object.keys doesn't find them.

is there reasonable way loop through keys of pos.coords?

you can in fact loop through them:

for (const p in pos.coords)     console.log(p); 

an alternative use

object.getownpropertynames(object.getprototypeof(pos.coords)); 

do have guess why did way?

i suspect they're getters access internal native data representation linked host object. way, they're implicitly readonly the spec interface requires. should have been able achieve making them own, non-writable data properties either, though.





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -