【问题标题】:JavaScript console.log execution order?JavaScript console.log 执行顺序?
【发布时间】:2012-10-05 00:18:23
【问题描述】:

我的代码看起来像这样:

function pathfind (start,end,map)
{
    this.Init = function ()
    {
       this.open_node = new Array();
       this.open_node.push(start);
       console.log(this.open_node);
       this.Loop();
    }
    this.Loop = function ()
    {
       //Some code here
    }
    this.Init();
}

由于某种原因,当我将“开始”推送到 this.open_node 并记录其值时,我得到“未定义”。但是,经过一些错误测试后,我意识到注释掉 this.Loop();在 this.Init 中,push 正常运行,console.log 返回 [start]。谁能解释为什么会发生这种行为?

编辑:我在打电话

pathfind({x:2,y:2},{x:24,y:24},parsemap(25,25));

【问题讨论】:

  • 调用 pathfind() 的行在哪里。你所传递的可能会产生影响......
  • 感谢您指出这一点。我的测试表明数据类型无关紧要,但你可以这样做。
  • 如果你在 console.log 语句之前放置一个断点会发生什么?
  • 无效。我应该意识到 Chrome 控制台是异步的,所以不会很好玩。

标签: javascript console execution


【解决方案1】:

经过进一步研究,我发现 console.log 不会立即在 Chrome 中执行。因此,过时的报告。

【讨论】:

    【解决方案2】:

    您的代码执行返回undefined 的pathfind 函数(应该是这种方式),但您等待来自this.Init 函数的结果。可能应该执行它而不是 pathfind。

    【讨论】:

    • 对不起,我省略了重要的代码行(this.Init();)。我进行了更多研究,发现与警报不同,console.log 不会立即执行。去搞清楚。 :\
    猜你喜欢
    • 2012-11-23
    • 1970-01-01
    • 1970-01-01
    • 2016-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多