Why does console.log (window) work, but JSON.stringify (window) does not work, and how can I defeat this?

If I type the console:

 console.log(window)

I get all the objects in windowusing the extension buttons.

But if I try the same:

JSON.stringify(window) 

I get in Firefox:

Error: Permission denied to access property 'toJSON'

And in chrome:

TypeError: Converting circular structure to JSON

Is this the only time this happens? And given that console.log()they JSON.stringify()work differently, can I access and still create objects that I console.log()can display?

+4
source share
1 answer

, window (, window.self window), JSON, .

, window:

var foo = {
    bar: 'bar'
};
JSON.stringify(foo);    // works fine

var foo = {
    bar: foo
};
JSON.stringify(foo);    // circular reference -> crashes
+4

Source: https://habr.com/ru/post/1525250/


All Articles