简体   繁体   中英

How can i get a variable in anonymous function from jQuery scope when I use chrome console?

I'm a beginner in jQuery, I learned and write code in this style from a document, I want to see the data in that scope when I use chrome console,but I can't.I want to use some tools or code to get that variable.

(function($){
    $(function(){
        var a = 1;
        //code
        //'that' is this scope
    })
})(jQuery);

There are two ways to see the value of a on the Chrome console:

  1. Use the debugger built into Chrome to set a breakpoint on any line of code in that inner function. When the breakpoint is hit, code in the console is run within the scope of the code at the breakpoint, so you could use the console to inspect a . (Or you could just hover over a in the source window to see its value.)

  2. Add a line of code to the function: console.log(a) . But I recommend using the debugger instead.

TJ Crowder's answer actually provides a good solution, here I would like to add the use of debugger statement which invokes any available debugging functionality (similarly on settings a breakpoint in your code).

If no debugging functionality is available or your browser dev tools are closed, this statement has no effect.

   (function($){
        $(function(){
           debugger
            var a = 1;
            //code
            //'that' is this scope
        })
    })(jQuery);

To learn more about debbuggin in chrome:

https://developer.chrome.com/devtools

More on debugger :

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Statements/debugger

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