隐含地全局“项目”变量 - Internet Explorer和FireFox之间的区别 [英] Implicitly global "item" variable - difference between Internet Explorer and FireFox

查看:103
本文介绍了隐含地全局“项目”变量 - Internet Explorer和FireFox之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有这个JS代码:

  var someExternalArray = [{id:1,name:'a'},{id:2,name:'b'},{id:3,name:'c'}]; 
var newArray = []

// var item;
for(var i = 0; i< someExternalArray.length; i ++){
item = new Object();
item.id = someExternalArray [i] .id;
item.name = someExternalArray [i] .name;
newArray.push(item);
}

alert('0:'+ newArray [0] .name +',1:'+ newArray [1] .name +',2:'+ newArray [2] 。名称);

请注意注释 var item 循环隐式声明 item 变量。




  • 如果我在FireFox上运行此代码,alert的结果是: 0:a,1 :b,2:c


  • 如果我在Internet Explorer中运行相同的代码,结果为:
    0:c,1:c,2:c




这里是jsfiddle: https://jsfiddle.net/fvu9gb26/



当然,当我取消注释 var item 时,它在每个浏览器中的工作方式都是相同的。



<有人知道为什么会出现这种差异吗?基本上,这是因为Internet Explorer的窗口对象公开了

解决方案您的脚本无法覆盖的 item()方法。

  item = new Object(); $> 


item 未在本地范围内声明,所以它被解释为全局对象的一个​​属性( window.item )。在Firefox上, window 不公开一个名为项目的成员,因此引入了一个新成员,并且 new object()被分配给它。



另一方面,Internet Explorer公开了一个名为 window.item()。该成员不可写入,因此分配不能发生 - 它被默默忽略。换句话说, item = new Object()根本没有任何作用(当然,它确实创建了一个对象,但是之后不能分配它)。



后续赋值给 id name 最终创建了 item()方法。它们始终是同一个方法的相同成员,所以它们的值在每次循环迭代时都被覆盖。另外,每次迭代都会将相同的对象( item()方法)推送到数组中。

因此,数组最终包含三次相同的对象,并且它的 id 名称成员的值是最后一个分配给它们的值(在最后一次迭代中)分别为 3 'c'


Just out of curiosity..

I have this JS code:

var someExternalArray = [{id: 1, name: 'a'}, {id: 2, name: 'b'}, {id: 3, name: 'c'}];
var newArray = []

//var item;
for (var i = 0; i < someExternalArray.length; i++){
    item = new Object();
    item.id = someExternalArray[i].id;
    item.name = someExternalArray[i].name;
    newArray.push(item);
}

alert('0:' + newArray[0].name + ',1:' + newArray[1].name + ',2:' + newArray[2].name);

Notice the commented var item which leaves the loop with implicitly declared item variable.

  • If I run this code on FireFox, the result of alert is: 0:a,1:b,2:c

  • If I run the same code in Internet Explorer, the result is: 0:c,1:c,2:c

Here is jsfiddle: https://jsfiddle.net/fvu9gb26/

Of course, when I uncomment the var item it works the same way in every browser.

Does anyone know why this difference occurs? Thank you.

解决方案

Basically, that's because Internet Explorer's window object exposes an item() method that your script cannot overwrite.

In the line:

item = new Object();

item is not declared in the local scope, so it is interpreted as a property of the global object (window.item). On Firefox, window does not expose a member named item, so a new member is introduced and the result of new Object() is assigned to it.

On the other hand, Internet Explorer exposes a native method named window.item(). That member is not writeable, so the assignment cannot take place -- it is silently ignored. In other words, item = new Object() has no effect at all (well, it does create an object but cannot assign it afterwards).

The subsequent assignments to id and name end up creating new members of the item() method. These are always the same members of the same method, so their values are overwritten on every loop iteration. In addition, the same object (the item() method) is pushed to the array on every iteration.

Therefore, the array ends up containing three times the same object, and the values of its id and name members are the last values assigned to them (in the last iteration), respectively 3 and 'c'.

这篇关于隐含地全局“项目”变量 - Internet Explorer和FireFox之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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