简体   繁体   English

使用Chrome控制台时,如何从jQuery范围中获取匿名函数中的变量?

[英]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. 我是jQuery的初学者,我从文档中学习并编写了这种风格的代码,我想在使用chrome控制台时查看该范围内的数据,但我不能。我想使用一些工具或代码来得到那个变量。

(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: 有两种方法可以看到的价值a在Chrome的控制台上:

  1. Use the debugger built into Chrome to set a breakpoint on any line of code in that inner function. 使用Chrome内置的调试器在该内部函数的任何代码行上设置断点。 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 . 当断点被击中,在控制台代码在断点代码的范围内运行,所以你可以使用控制台检查a (Or you could just hover over a in the source window to see its value.) (或者您也可以悬停在a源窗口中看到它的价值。)

  2. Add a line of code to the function: console.log(a) . 向该函数添加一行代码: 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). TJ Crowder的答案实际上提供了一个很好的解决方案,在这里我想补充一下debugger语句的使用,该语句调用任何可用的调试功能(类似地,在代码中设置断点)。

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: 要了解有关Chrome中的debbuggin的更多信息:

https://developer.chrome.com/devtools https://developer.chrome.com/devtools

More on debugger : 有关debugger更多信息:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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