0

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);
2
  • Please tell us waht you really want to do, so we can provide the best possible example. There are different ways, but not all are good to use ... Commented Aug 25, 2016 at 7:14
  • "I can't get the variable in that scope in chrome console." Indeed you can't. Neither can anyone else. Use a breakpoint (debugger;) to halt execution where you want so you can inspect it then and there, or assign to a global variable which will be available later in the console. Commented Aug 25, 2016 at 7:16

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

T.J. 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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.