在 Chrome 控制台中检查 JS 变量的值

Inspecting the value of a JS variable in Chrome console

这是我在我的网站上使用的 JS 插件的片段。我想直接在控制台中检查 isTouchDevice 的值。只需键入 isTouchDevice 就会返回 'undefined' 错误。

(function($) {

    "use strict";

    $.maxmegamenu = function(menu, options) {

        var plugin = this;

        var $menu = $(menu);

        plugin.settings = {};

        var isTouchDevice = function() {
            return ('ontouchstart' in window || navigator.maxTouchPoints > 0 || navigator.msMaxTouchPoints > 0);
        };
})(jQuery);

如何在 Chrome 控制台中检查 isTouchDevice 变量的值?

编辑 1:

澄清一下,首先,我需要能够直接在控制台中检查 var isTouchDevice 的值,而无需添加任何代码。其次,console.log(isTouchDevice) 会 return 一个 'undefined error' 可能是由于变量封装(它在全局范围内不可用)所以只是控制台日志记录不是一种方法。

使用console.log(isTouchDevice);

或其他 console 方法,例如 .info .warn .error .debug

或者您可以使用 console.group('this is some event'); 分组,然后使用 console.groupEnd();

关闭分组

最后一个方法是console.groupCollapsed('some title of group');,会创建组,但会在控制台折叠,消耗较少space

您还可以在 maxmegamenu 函数的末尾添加一个 debugger; 语句。

当 chrome 在该语句上中断时,控制台将在该函数的上下文中,然后应定义 isTouchDevice