Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why doesn't getBoundingClientRect serialise with JSON.toStringify?

Try this in dev tools console:

JSON.stringify(document.body.getBoundingClientRect())

the output is {}, instead of something sensible.

Any ideas?

like image 208
SuperUberDuper Avatar asked Oct 29 '22 20:10

SuperUberDuper


1 Answers

JSON.stringify internally uses a method called toJSON.

You can specify it for your object or in your case, override it:

ClientRect.prototype.toJSON = function(){ return { top: this.top } }

JSON.stringify(document.body.getBoundingClientRect())

"{"top":-583}"
like image 149
johnnydev Avatar answered Nov 11 '22 05:11

johnnydev