简体   繁体   中英

How to debug JavaScript closure state?

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)?

在此处输入图片说明

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM