1

Is there a way to find out where (in which function or in which file) a javascript object gets created or a variable gets defined?

I am working on a website where a variable foo gets created and is available in the chrome dev tools console when I add a div with the id foo. But I can not find out where that happens. It has to be in one of the plugins.

gang
  • 1,758
  • 2
  • 23
  • 36
  • Try this post see if it helps http://stackoverflow.com/questions/858181/how-to-check-a-not-defined-variable-in-javascript – Alex Oct 22 '13 at 14:32

1 Answers1

3

The browser creates global vars for every html tag with an id or name, so if you have a <div id="foo" /> the browser will create a foo object that references the div, just as if you did var foo = document.getElementById('foo');

Nicolas Straub
  • 3,381
  • 6
  • 21
  • 42
  • Firefox doesn't do this, but you're right that others (Chrome in particular) do. – Pointy Oct 22 '13 at 14:36
  • 1
    It's a really terrible thing that they do it, too :) – Pointy Oct 22 '13 at 14:36
  • @Pointy, I couldn't agree with you more... implicit global variable creation is probably one of the worst headscratchers in JS and browsers – Nicolas Straub Oct 22 '13 at 14:38
  • Ha ha yes, though I've come to think of it as a great motivation to write most code inside closures :) – Pointy Oct 22 '13 at 14:39
  • @Pointy true. BTW I tested your comment in firefox (latest version) and apparently it does create a variable for each element. you can see the example here: http://jsfiddle.net/dL96d/ – Nicolas Straub Oct 22 '13 at 14:41
  • Wow, that must be somewhat new (or else my brain is broken?). I think my brain is OK because there are a bunch of old SO questions regarding code that worked in IE and Chrome but not Firefox, and that was the reason. – Pointy Oct 22 '13 at 15:04