代码之家  ›  专栏  ›  技术社区  ›  Deadpool

控制台中的混乱或者如何通过查看一个项是数据类型未定义,还是字符串未定义来查找,Google应该改进字符串控制台吗?

  •  1
  • Deadpool  · 技术社区  · 4 年前

    undefiend (但以字符串格式)存储在 var x . 现在,我们知道了 "undefined" !== undefined ,因为它们是两种不同的数据类型。

    "undefined" 在控制台中使用双引号(以消除混淆)。这是Google的开发人员遗漏的东西,还是他们不在乎,或者他们故意让这种行为(在控制台中不加引号的字符串)发生??我很困惑。请提出你的看法。

    var y = 1, x = y = typeof x;
    console.log(x);
    console.log(x === undefined);
    console.log(x === "undefined");
    console.log("undefined");

    a = "Peter"; b = "Jacob";
    console.log(a, b, "Ronald");
    1 回复  |  直到 4 年前
        1
  •  3
  •   CertainPerformance    4 年前

    这就是所有记录的字符串在Chrome中呈现的方式:黑色,没有分隔符。

    价值 undefined 是以颜色区分的,但有点难看-不像弦, 该值略为灰色,而不是黑色:

    console.log("undefined");
    console.log(undefined);
    <img src="https://i.stack.imgur.com/kBkAp.png">

    是的,在我看来,这绝对是显而易见的。

    一个奇怪的黑客,你可以实施,使它更清楚,将补丁 console.log apply CSS of your own 当值为 未定义

    const origConsoleLog = console.log;
    console.log = (...args) => {
      if (args.length > 1) origConsoleLog(...args);
      const [arg] = args;
      
      // customize...
      if (arg === undefined) origConsoleLog('%cundefined', 'font-weight: bold; color: grey');
      else origConsoleLog(arg);
    };
    
    console.log("undefined");
    console.log(undefined);
    <img src="https://i.stack.imgur.com/qFFMC.png">

    如果需要的话,可以将其应用于所有带有用户脚本的站点,这样就不必每次都添加它。