仅使用Array.push时,数组中的单元化变量? [英] Unitialized variables in an array when using only Array.push?

查看:76
本文介绍了仅使用Array.push时,数组中的单元化变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个相当有趣的问题。下面的代码生成一个数组,当记录时,它给出值 [undefined×1] 。根据此答案,这意味着该阵列具有单元化索引 - 在这种情况下,只有一个。但是,由于我只使用 array.push()来避免这种问题,我有点迷茫。据我所知,只分配索引至少一个索引大于其他初始化索引可能导致问题。无论如何,这是代码:

I've run into a rather interesting problem. The code below generates an array that, when logged, gives the value [undefined × 1]. According to this answer, that means that the array has unitialized indexes--in this case, only one. However, since I'm only using array.push() to avoid exactly this sort of problem, I'm kind of lost. To the best of my knowledge only assigning an index at least one index greater than other initialized indexes could cause the problem. Anyhow, here's the code:

function Thing(params) {
    var that = this;

    this.OtherObject = function OtherObject(param) {
        this.param = param;
    }

    this.Foo = function Foo() {
        this.bar = [];

        this.add = function(item) {
            if (this.bar.length != 0) {
                for (var i = 0, len = this.bar.length; i < len; i++) {
                    if (this.bar[i].param <= item.param) {
                        this.bar.splice(i, 0, item);
                    }
                }
            } else {
                this.bar[0] = item;
                console.log(this.bar);
            }
        }

        this.pop = function() {
            return this.bar.pop();
        }

        this.length = function() {
            return this.bar.length;
        }
    }

    this.someFunc = function() {
        var myThing = new that.Foo();
        myThing.add(new that.OtherObject(3));

        while (myThing.length() > 0) {
            var someVar = myThing.pop();
        }
    }
}

var myThing = new Thing({
    myParam: "someValue"
});

myThing.someFunc();
​

我创建了一个测试用例,看看我是否可以缩小问题范围,但是我最后重新输入了95%的代码,直到我再次遇到问题,这似乎与我使用 myThing.pop()有关。您可以在这里找到一个小提琴,它可以演示相关问题。

I created a test case to see if I could narrow the problem down, but I ended up re-typing 95% of my code until I ran into the issue again, which appears to be related with my use of myThing.pop(). You can find a fiddle here which demonstrates the issue in question.

那么......关于可能导致这种情况的任何想法?这实际上是我嵌套对象的方式的问题吗? (我按照这个答案给我的一个模式做了我之前的问题)。

So... any ideas as to what could be causing this? Is this actually a problem with the way I've nested my objects? (which I did following a pattern given to me in this answer to a previous question of mine).

如果你想知道我可能在这里尝试做什么,我将外部对象视为一种命名空间,我正在做避免混乱全局命名空间(如果我在这里一起行动,这最终会成为一个寻路库)。 Foo 对象是一种有序列表,这样我就可以获得最近的节点(具有最低 param c)c> pop()堆栈。

In case you're wondering what I could possibly be trying to do here, I'm treating the outside object like a sort of namespace, which I'm doing to avoid cluttering up the global namespace (this will eventually be a pathfinding library, if I get my act together here). The Foo object is meant to be a sort of ordered list such that I can get the closest node (the one with the lowest param property) by pop()ing the stack.

推荐答案

谷歌Chrome的 console.log 功能正在发生一些奇怪的事情。其他浏览器似乎表现得像您期望的那样。这是我见过的最奇怪的事情之一:如果在调用 console.log alert 的调用c $ c>,控制台中会有不同的输出。

There is just something going on that is very strange with Google Chrome's console.log function. Other browsers seem to behave as you would expect. This is one one of the weirder things I've seen: if you add a call to alert after the lne that calls console.log, there will be different output in the console.

console.log(this.bar);
alert(this.bar);

// [> OtherObject]

如果你删除警告,如你所见,控制台将输出

If you remove the alert, as you have seen, the console will output

// [undefined × 1] 

这种情况要么是由于一些竞争条件还是一些副作用导致警告功能正在引起。这是关于幕后发生的事情的理论:

This sort of thing is either due to some race condition or some side effect that the alert function is causing. Here's my theory on what's going on behind the scenes:


  • console.log 函数实际上不会在控制台中记录值,直到代码中有一些停顿,然后它会记录所有排队的消息。队列包含对要记录的消息的引用。

  • 弹出数组时, this.bar 中的第一项将被销毁。除非存在中断,否则记录器将在销毁引用值后显示消息。因此,它显示 [undefined×1]

  • 调用 alert 会导致在代码中停止,以便控制台可以在删除引用值之前记录消息。

  • The console.log function does not actually log values in the console until there is some halt in the code, and then it logs all the queued messages. The queue contains references to the messages that are to be logged.
  • The first item in this.bar is destroyed when you pop the array. The logger, unless there is some interruption, will display the message after the referenced value is destroyed. Thus, it displays [undefined × 1]
  • Calling alert causes a halt in the code so that the console can log the message before the referenced value is erased.

我仍然不完全确定这真的是发生了什么,但似乎是合理的。我会做一些测试来检查我的理论是否正确。

I'm still not completely sure if this is really what's happening, but it seems to be somwhat reasonable. I'll do some more testing to check if my theory is correct.

这篇关于仅使用Array.push时,数组中的单元化变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆