If I type in console:
如果我输入控制台:
console.log(window)
I get all the objects in window
with expand buttons.
我用扩展按钮获取窗口中的所有对象。
But if I try the same with:
但如果我尝试相同的:
JSON.stringify(window)
I get in Firefox:
我进入Firefox:
Error: Permission denied to access property 'toJSON'
And in chrome:
在铬:
TypeError: Converting circular structure to JSON
Is this the only case where this happen? And given that console.log()
and JSON.stringify()
work differently, can I access and still stringify objects that console.log()
manages to display?
这是发生这种情况的唯一情况吗?并且鉴于console.log()和JSON.stringify()的工作方式不同,我可以访问并仍然对console.log()管理显示的对象进行字符串化吗?
1 个解决方案
#1
4
That's because window
has circular references (for instance, in most cases window.self
refers to window
) and then it cannot be converted to JSON, otherwise it would turn into an infinite loop.
那是因为窗口有循环引用(例如,在大多数情况下window.self引用窗口)然后它不能转换为JSON,否则它将变成无限循环。
This may happen on any object, not only on window
:
这可能发生在任何对象上,而不仅仅发生在窗口上:
var foo = {
bar: 'bar'
};
JSON.stringify(foo); // works fine
var foo = {
bar: foo
};
JSON.stringify(foo); // circular reference -> crashes
#1
4
That's because window
has circular references (for instance, in most cases window.self
refers to window
) and then it cannot be converted to JSON, otherwise it would turn into an infinite loop.
那是因为窗口有循环引用(例如,在大多数情况下window.self引用窗口)然后它不能转换为JSON,否则它将变成无限循环。
This may happen on any object, not only on window
:
这可能发生在任何对象上,而不仅仅发生在窗口上:
var foo = {
bar: 'bar'
};
JSON.stringify(foo); // works fine
var foo = {
bar: foo
};
JSON.stringify(foo); // circular reference -> crashes