为什么JSON.stringify仅显示click事件的isTrusted成员? [英] Why does JSON.stringify only show the isTrusted member of a click event?
本文介绍了为什么JSON.stringify仅显示click事件的isTrusted成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
HTML:
<button onclick="foo(event);">Test</button>
JavaScript:
Javascript:
window.foo = function(event) {
console.log(JSON.stringify(event));
}
控制台结果:
{"isTrusted":true}
正在发生在Chrome上。我还没有测试其他浏览器。
It is happening on Chrome. I haven't tested other browsers yet.
推荐答案
出于多种原因,某些属性未包含在JSON中。字符串化:
There are a number of reasons why some properties do not get included in JSON.stringify:
- 它们可能是
函数
s,无法进行字符串化 - 它们可能属于对象的原型(即类),而不是直接属于对象本身。
- They might be
function
s, which cannot be stringified - They might belong to the prototype (i.e. class) of an object, rather than directly belonging to the object itself.
如果您需要包括其他数据,最好的选择是手动构造一个包含要包含的内容的新对象:
If you need to include extra data, your best bet is to manually construct a fresh object with the things you want to include:
window.foo = function(event) {
console.log(JSON.stringify({keyCode: event.keyCode));
}
这篇关于为什么JSON.stringify仅显示click事件的isTrusted成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文