当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - 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?

网友答案:

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}"
分享给朋友:
您可能感兴趣的文章:
随机阅读: