0

For example, window.location is an original browser-defined property. But if I say window.test = "Hello"; that is a global variable, set by my custom JavaScript. Is there a way in JavaScript to differentiate between the two?

For context, my ultimate goal is to output a list of global variables, for debugging purposes but something like Object.keys(window); outputs both the browser-defined properties and the custom ones.

allicarn
  • 2,859
  • 2
  • 28
  • 47
  • 2
    No. There's no user-visible way to tell the difference. – Jared Smith Mar 15 '18 at 14:30
  • they're both global, your browser doesn't know who's set them. You can do all kinds of stuff to be done with scopes though. Or if you'd really want to; make a copy of the window object _before_ you do anything with it, and check which variables are new by comparing the current `window` object with the original one – giorgio Mar 15 '18 at 14:33
  • I mean, you could try deleting them (e.g., `delete window.location` will return false), but I wouldn't rely on that. – Heretic Monkey Mar 15 '18 at 14:34
  • 1
    Thanks for pointing me to the dupe! I found the answer I needed here: https://stackoverflow.com/a/17246535/738394 – allicarn Mar 15 '18 at 14:39

0 Answers0