5

This question is so basic, yet I have no idea of the answer.

Why screen object when stringified returns empty?

Does this mean JSON.stringify() needs read/write access to the input?

let a = {foo: 'one', bar: 2};


console.log(JSON.stringify(a));
console.log(JSON.stringify(screen));
Lemures
  • 474
  • 5
  • 11
  • 1
    @Variable https://developer.mozilla.org/en-US/docs/Web/API/Window/screen . If it was never declared you'd get an error about it being undefined. – ADyson Jul 27 '18 at 11:10
  • 7
    Possible duplicate of [Stringify DOMWindow object](https://stackoverflow.com/questions/4079653/stringify-domwindow-object) – user202729 Jul 27 '18 at 11:11
  • @ADyson Thanks, didn't knew about this, learned something new :D. – node_modules Jul 27 '18 at 11:17
  • @user202729 I think the answer here is better than the one in the duplicate. I'll change the tittle so that it's more general. – Lemures Jul 27 '18 at 11:29

1 Answers1

11

FROM MDN Network

For all the other Object instances (including Map, Set, WeakMap and WeakSet), only their enumerable properties will be serialized.

Read https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/propertyIsEnumerable

    console.log((window.screen));
    console.log(JSON.stringify(window.screen));
    console.log(window.propertyIsEnumerable(screen));
Skeets
  • 4,476
  • 2
  • 41
  • 67
Kapil Tiwari
  • 334
  • 2
  • 7
  • 3
    Actually, most of the (popular) answers on this site are just a docs-quote. That is a proper answer (as long as you quote important parts and not just a link) – user202729 Jul 27 '18 at 12:56