最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Inspecting the value of a JS variable in Chrome console - Stack Overflow

programmeradmin0浏览0评论

This is fragment of a JS plugin that I'm using on my site. I'd like to inspect the value of isTouchDevice directly in the console. Just typing isTouchDevice reurns an 'undefined' error.

(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);

How I can inspect the value of isTouchDevice variable in Chrome console?

Edit 1:

To clarify, First, I need to be able to inspect the value of var isTouchDevice directly in the console, without adding any code. Second, console.log(isTouchDevice) would return an 'undefined error' probably due to the variable encapsulation (it's not available in the global scope) so just console-logging it is not a way to go.

This is fragment of a JS plugin that I'm using on my site. I'd like to inspect the value of isTouchDevice directly in the console. Just typing isTouchDevice reurns an 'undefined' error.

(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);

How I can inspect the value of isTouchDevice variable in Chrome console?

Edit 1:

To clarify, First, I need to be able to inspect the value of var isTouchDevice directly in the console, without adding any code. Second, console.log(isTouchDevice) would return an 'undefined error' probably due to the variable encapsulation (it's not available in the global scope) so just console-logging it is not a way to go.

Share Improve this question edited Nov 26, 2015 at 15:41 Sadegh 4101 gold badge23 silver badges48 bronze badges asked Jun 3, 2015 at 14:56 luqo33luqo33 8,39120 gold badges60 silver badges108 bronze badges 5
  • 2 For simplicity, you can just run ('ontouchstart' in window || navigator.maxTouchPoints > 0 || navigator.msMaxTouchPoints > 0) in the chrome console. Otherwise you can put a breakpoint after declaration and add a watch in your dev tools – CodingIntrigue Commented Jun 3, 2015 at 15:03
  • note that isTouchDevice in your code snippet is assigned with a function. to get the value you need to access it by invoking it console.log(isTouchDevice()); you also need to call it in the right scope which is inside the block var isTouchDevice is declared – lid Commented Jun 3, 2015 at 15:06
  • @RGraham, this is a great remendation which solves my problem - executing the expression directly in the console.. – luqo33 Commented Jun 3, 2015 at 20:07
  • @lid Just out of curiousity as my probelm has already been solved. Due to scoping, I will not be able to inspect isTouchDevice() directly in the console? – luqo33 Commented Jun 3, 2015 at 20:08
  • @luqo33, that is right, to have access to the method you need to be in the right scope chain. see: gist.github./anonymous/db528b893d477b49775a hypothetically if you need to run arbitrary code within a particular scope/conext in console, the approach eck mentioned is the way to go about it. – lid Commented Jun 3, 2015 at 23:11
Add a ment  | 

2 Answers 2

Reset to default 3

You can also add a debugger; statement at the end of your maxmegamenu function.

When chrome breaks on that statement the console will be within the context of that function and isTouchDevice should be defined then.

Use console.log(isTouchDevice);

or other console methods like .info .warn .error .debug

or you can use grouping with console.group('this is some event'); then close group with console.groupEnd();

last method is console.groupCollapsed('some title of group');, what will create group, but will be collapsed in console, consuming less space

发布评论

评论列表(0)

  1. 暂无评论