views:

57

answers:

3

Are there any properties one can use or web tools so I could evaluate the scope of two javascript objects at runtime?

A: 

Yes, I use MS Visual Studio to observe the change of the scope. Normal I look at this keyword

unigg
+2  A: 

Not in a browser. The Rhino JavaScript platform gives you all kind of access to scopes and contexts though (through Java).

For what purpose do you need to access that scope?

If you want to execute a piece of code with access to properties of a certain object, you could always use eval and with (with their performance drawbacks 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 a certain content, without the object, just define a function inside that scope that you can execute from the outside.

For example:

var module = (function () {
   var a = 2;

   var peek = function (fn) {
      eval("("+fn+")()");
   }

   return {
      peek: peek
   }
})();

module.peek(function () { alert(a); });
CD Sanchez
A: 

In Opera Dragonfly (developer's tools) you can set breakpoints inside of scope of object and see variables, methods and objects available in that scope. I don't know how it is in other browser's tools (WebKit JavaScript Console, FireBug), but I think the mechanism is similar.

pepkin88