Frage

For example I have code like this:

$(function () {
    var a = {
        init: function () { ...

Is it possible to hook a in Firebug and call its methods within the command line?

War es hilfreich?

Lösung 2

I needed to do something similar on a project a while back -- I had some private values that I still needed to access from a QUnit test.

My solution was along these lines:

 (function() {
   var innerVars = {
     myvar1: 1,
     myvar2:
   };

   function myMethod() {
     console.log(innerVars.myVar1);
   }


   // Make my return value.
   var r = {
     myMethod: myMethod
   };

   if (window.QUNIT_TEST_IN_PROGRESS) {
     r.inner = innerVars;
   }

   return r;
 }();

Andere Tipps

Firebug offers closure variable inspection via its command line using .%. It also displays them within the DOM panel when the Show Closures option is checked.

A detailed description of how the closure inspector works can be found in the Firebug wiki:

https://getfirebug.com/wiki/index.php/Closure_Inspector

Variables inside a function or inside a closure are not directly accessible from outside the function.

The only way to access them would be if the code inside the function offers a publicly accessible method to retrieve that specific data because only the code inside the function can get access to them.

If this is just a regular function (doesn't form a closure of any kind), then the local variables in that function are only even "alive" during the lifetime of the function and will be garbage collected after the function finishes executing.


If you're just trying to debug something, you could set a breakpoint inside that function and from that breakpoint, you could set a global variable to point to something like a inside that function which you could then call at a later time by using the global variable reference. This would be a debugging aid only as it's not practical in normal executing JS without modifying the code inside the function.

You can just make a global variable and make it a reference to the variable you want to inspect.

$(function () {
    var a = {
        init: function () { ...

    // now make a global variable and reference 'a'
    window.test_a = a;

Then in your console, you should be able to do

test_a.init()

Obviously this would just be for debugging purposes, otherwise avoid using globals.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top