0

Let say I know that a variable is called "myVar", it is declared in a self calling function

(function (){
    var myVar=0;
    setInterval(function(){ 
            console.log(myVar++);
        }, 3000);  
})();

But I don't know where this function is called, and don't want to reverse engineer tons of JS. I want to find a reference to this variable so I can display it in console something like window.rootObject.subObject.myVar . I tried to make a function that does recursive seek for key from the window object, but it does a stackoverflow, certainly because of circular references (an object contain reference to its containing object).

Is there any simple way to find where is this object ?


Edit : the original question was about finding a var in global space having just its name, without knowing if it was descendent of window object or stored in EnvironementRecord. The sample code I provided was misleading, so I edited the question and accepted an answer to make it usefull.

3
  • The code sample above is meaningless. There's a locally-defined variable, but you've said it "belongs to an object". So, does it become a property of the object? Or is it just used in methods that are referenced by properties of the object? Or...? Commented Sep 8, 2016 at 12:40
  • I just guess all objects have a parent object. No ? I updated the exemple to show that myVar is persistent for a long time. Commented Sep 8, 2016 at 13:04
  • "I just guess all objects have a parent object. No ?" "Parent object" isn't a term with a well-defined meaning in JavaScript. If you mean "All objects are referenced by properties of other objects," then no, that's not true, at least not outside the internals of the JavaScript engine, which obviously does keep track of all objects with some form of structure. If you mean "All objects have a prototype" (which I don't think you do), that's also not true. Commented Sep 8, 2016 at 13:29

2 Answers 2

2

myVar in your updated example doesn't "belong" to any object you can get a reference to. In specification terms, it belongs to the EnvironmentRecord for the variable environment of the execution context for the call to the anonymous function, but there's no way for you to get a direct access to that object (which may or may not literally exist at runtime at all, depending on how the JavaScript engine being used is implemented).

You seem to believe that all objects (even specification-only objects like environment records) are in a tree and are acessible following a path of references from some root object and so are "discoverable" somehow in code. That's understandable, but it's not the case. Certainly the JavaScript engine has trees like that for garbage collection purposes (for objects that actually get created at runtime), but no, there's no root from which everything can be discovered that you can access in code.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, that was my question. I thought there might be a special object for debugging purpose which expose the tree (graph) of the garbage collector.
I should not have added this sample code, because at start I didn't knew if this var was completely local or attached in some way to the global scope. I made a more specific question : stackoverflow.com/questions/39392729/…
2

Locally scoped variables really are locally scoped. They are not accessible outside that scope at all. They are not properties that can be accessed through the window object.

If you want to debug code using the variable, set a breakpoint (e.g. by adding a debugger statement) inside the function to which the variable is scoped.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.