Search code examples
javascriptshadowing

How do I find where a variable is defined at runtime?


I've been using jQuery and YUI side-by-side with no issues until recently. Occasionally, inside of a callback for, say, a YUI button, $ will be shadowed by some other function (click for big version): console

and for the life of me, I cannot figure out why this is happening. Yes, I know I could be safe and use jQuery or window.$ everywhere instead of just $, but that's just a workaround and not an actual fix.

At runtime, how can I find where this $ impostor is coming from? - e.g. find where it's declared, and why it's in my bleedin' scope.


It turns out that this behavior is easily reproduced (at least in Chrome and Firefox 4) right here on Stack Overflow, since SO uses jQuery (again, click for full size):

more console still more console

I can only infer that $ as

function () {
    return document.getElementById.apply(document, arguments)
}

must be from the console itself!

Found it.

with strikes again.

enter image description here enter image description here

Chromium bug for this: http://code.google.com/p/chromium/issues/detail?id=70969


Solution

  • I'm betting this doesn't happen in IE? This is the only hint I could find:

    http://davidwalsh.name/dollar-functions

    http://dam5s.tumblr.com/post/3079779011/bug-in-chromes-javascript-console

    Some sort of bug in Chrome/Firefox/Safari.