Is there a way in javascript to get the scope of an object?

Are there any properties that can be used or web tools so that I can evaluate the scope of two javascript objects at runtime?

+1
source share
3 answers

Not in the browser. The Rhino JavaScript platform gives you all kinds of access to areas and contexts, though (via Java).

Why do you need to access this area?

If you want to execute a piece of code with access to the properties of a specific object, you can always use eval and with (with their performance flaws included).

 function exec(obj, func) { with (obj) { eval("("+func+")()"); } } var actObj = { annoying: function (txt) { alert(txt); } } // using it: exec(actObj, function () { annoying("HEY THERE FRIEND ! !"); }); 

If you want to execute code in specific content, without an object, just define a function inside this scope that you can execute from the outside.

For instance:

 var module = (function () { var a = 2; var peek = function (fn) { eval("("+fn+")()"); } return { peek: peek } })(); module.peek(function () { alert(a); }); 
+2
source

In Opera Dragonfly (developer tools), you can set breakpoints within the scope of an object and view the variables, methods, and objects available in that area. I do not know how this is done in other browser tools (WebKit JavaScript Console, FireBug), but I think the mechanism is similar.

+2
source

Yes, I use MS Visual Studio to observe the change in the area. Ok i'm looking at this keyword

+1
source

All Articles