【问题标题】:JavaScript Array `console.log` output behaviourJavaScript Array `console.log` 输出行为
【发布时间】:2017-07-04 13:35:15
【问题描述】:

当我们根据indexitems 的排列方式在数组上运行forEach() 函数时,开发工具会返回不同的输出。我想知道是否有人知道为什么会发生这种情况。

请看下面的图片:

当我将变量从 item, index 交换到 index, item 时,输出看起来不同。

我想知道是什么让变量输出不同以及为什么。

【问题讨论】:

  • 请注意:我只在 Chrome 中看到这种行为。 Firefox 在这两种情况下没有区别。您还可以将示例缩短为 console.log( "a", 1 ) vs console.log( 1, "a", )
  • 我不明白这个问题。为什么在交换参数时对输出的变化感到困惑?
  • 没有区别。您只是像这样在控制台中打印它们。
  • @Candide 我认为这是一个很好的问题,尽管它看起来微不足道。
  • 这与forEach无关。请edit 您的问题包括您正在使用的console 实现(什么JS 环境)。如果您可以将代码/输出作为文本而不是绘画发布,这将有所帮助。

标签: javascript console console.log


【解决方案1】:

根据 MDN 文档关于 console.log here

这解释了第一个参数被处理为对象或字符串。后续参数作为对象处理。

假设这会强制字符串,从第二个参数开始,用引号打印。

要了解有关 chrome 控制台的更多信息,请访问 this page

【讨论】:

    【解决方案2】:

    正如here 所说的

    Syntax
    console.log(obj1 [, obj2, ..., objN]);
    console.log(msg [, subst1, ..., substN]);
    Parameters
    
    obj1 ... objN
    A list of JavaScript objects to output. The string representations of each of these objects are appended together in the order listed and
    

    输出。

    当你写 consoloe.log(item,index) 时它先写 item 然后 index 反之亦然

    【讨论】:

      猜你喜欢
      • 2015-05-30
      • 2022-01-13
      • 1970-01-01
      • 1970-01-01
      • 2018-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多