English 中文(简体)
仅使用 array. push 时, 在数组中的合并变量吗?
原标题:Unitialized variables in an array when using only Array.push?
  • 时间:2012-05-26 02:59:05
  •  标签:
  • javascript

我遇到了一个相当有趣的问题。 下面的代码生成了一个数组, 当登录时, 给出了值 < code> [ 未定义 x1] 。 根据< a href=" https:// stackoverflow.com/ a/ 10683811/ 339852" > 的回答 < /a>, 这意味着数组已经将指数- 在此情况下, 只有一个。 但是, 由于我仅仅使用 < code> array. push () 来避免这种问题, 我损失了 m 。 据我所知, 我最了解的只是指派一个比其它初始索引更大的索引, 可能会造成问题。 无论如何, 这里的代码是 :

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%的代码,直到我再次遇到这个问题,这似乎与我使用 my Thing.pop () 有关。你可以找到一个小提琴 < a href=> "http://jsfiddle.net/cbYqG/3/" rel=“nofollown noreferrer'>这里的“/a>,这表明了这个问题。

那么... 任何关于是什么原因的原因的想法吗? 这实际上是否与我筑巢的方式有问题? (我这样做是因为我用的“https://stackoverflow.com/a/100511156/339852>对我之前的问题的回答 。) (我这样做时采用了在上写给我的模式 ) 。

如果您想知道我可以在这里做什么, 我将外部对象处理成一种命名空间, 我这样做是为了避免折叠全球命名空间( 如果我在这里将我的行为集中在一起, 这最终将是一个路由调查库 ) 。 < code> Foo 对象的意思是排序列表, 这样我就可以通过堆叠中的 < code>pop () 属性获得最接近的节点( 最小的 < code> param 的节点) 。

最佳回答

Google Chrome s connoole.log 函数中有些事情非常奇怪。其他浏览器的行为似乎如您所期望的那样。这是我所见过的更奇怪的事情之一:如果你在调用 connole.log 后添加呼叫ward的呼叫ward,主控台将出现不同的输出。

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

// [> OtherObject]

如果您已经看到,删除 提醒 , 控制台将输出

// [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.

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

问题回答

暂无回答




相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签