4

I have this code snippet that scientifically accurate calculates the speed of a cat according to the amount of legs it has.

function Cat()
{
    var self = this;

    var _legs = 0;

    self.addLeg = function()
    {
        _legs++;
    }

    self.speed = function()
    {
        return Math.pow(_legs,1.5)*2;
    }
}

Chrome Debugger

When I try to debug one of my cats, how can I see the inner variables, like _legs:

  • without adding extra code just to expose it,
  • nor 'actively' executing functions in the cat (that might change state)?

enter image description here

Dirk Boer
  • 8,522
  • 13
  • 63
  • 111

1 Answers1

0

You can't expose a variable without exposing it. You should, however, be able to see the state of _legs while stepping through the constructor, addLeg and speed functions.

DoronG
  • 2,576
  • 16
  • 22
  • Do you know if this is a theoretical limit because how the language works, or is this just a limit in the interface? – Dirk Boer Feb 03 '15 at 08:23
  • looks like an "interface" limit, though an expected one.The chrome console interface reflects properties and methods of an object. A closure scoped variable is not tied to the object, but to the function's scope context. By the way, the same thing happens in C#-Visual studio as well, where you create a closure with a variable and try to use the debugger to look at the containing object. So even though I can think of ways to display the variable in the chrome console it would not make sense. Also, think about the fact that you may have multiple contexts for the same function (i.e. Running the f – DoronG Feb 03 '15 at 13:27