问题
HTML:
<button onclick="foo(event);">Test</button>
Javascript:
window.foo = function(event) {
console.log(JSON.stringify(event));
}
Console Result:
{"isTrusted":true}
It is happening on Chrome. I haven't tested other browsers yet.
回答1:
There are a number of reasons why some properties do not get included in JSON.stringify:
- They might be
functions, 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));
}
来源:https://stackoverflow.com/questions/41020307/why-does-json-stringify-only-show-the-istrusted-member-of-a-click-event