Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 hereenter image description here

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

like image 722
Matt Ball Avatar asked Feb 12 '11 00:02

Matt Ball


People also ask

How do you check if a variable has been defined?

The typeof operator will check whether a variable is defined or not. The typeof operator doesn't throw a ReferenceError exception when it is used with an undeclared variable. The typeof null will return an object. So, check for null also.

What is a runtime variable?

Runtime variables are used to mark the attributes while creating the blueprint so that those attributes can be modified at the time of launching the application blueprint.

How do you define a runtime variable in Python?

In Python,we can define a python function at runtime execute with the help of FunctionType(). First we import types module then perform compile() function and pass parameter exec and after that with the help FunctionType() define the function at runtime. Example 1: Function to print GEEKSFORGEEKS.

What type of variable is running time?

Time is considered an interval variable because differences between all time points are equal but there is no “true zero” value for time.


2 Answers

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.

like image 102
anon Avatar answered Oct 20 '22 22:10

anon


Here's something to try. Firebug's debugger shows you all the variables that are available at a scope, in your example, it's obviously not local scope, and it's also not global since you already tested window.$. Therefore, it's has(?) to be a closure variable (which should be in the same file).

The attached screenshot shows the closure scopes that are available Firebug debugger

The example shows that data is available within $.each as a closure variable. Again, according to my theory, you should be able to find the culprit by looking for instances of $ on the page, since closures are lexically defined . Maybe there's a self calling function that passes in $ like jquery plugins.

like image 20
Juan Mendes Avatar answered Oct 20 '22 21:10

Juan Mendes