javascript - 在 IE11 中循环

标签 javascript internet-explorer

IE 11 中的控制台

enter image description here

Chrome 中的控制台

enter image description here

如果我像这样将循环中的单词“item”更改为“anotherItem”

var obj = {
    id1: 'item 1',
    id2: 'item 2',
    id3: 'item 3'    
};
for (anotherItem in obj){
    console.log(anotherItem);
}

循环工作正常

为什么 IE 11 不处理单词“item”

最佳答案

item 在 IE 中被定义为 native 函数,并且可能是只读的,因此您不能更改它的值。

在 Edge 之前,Microsoft 不喜欢遵守标准,并引入了标准中没有的各种功能。 item 函数在 Edge 中不存在。

此外,您还没有声明 anotherItem,试试这个:

试试这个:

var obj = {
    id1: 'item 1',
    id2: 'item 2',
    id3: 'item 3'    
};

for (var anotherItem in obj){
    console.log(anotherItem);
}

如果您不使用 var 键声明变量,并且您未处于严格模式,它将被定义为全局变量(这不是您想要的)。全局变量本质上是全局对象的属性,在网络浏览器的上下文中,它是 window 对象。

将以下内容添加到您的 JS 文件的顶部以启用严格模式,然后您将无法首先犯这些错误,因为会抛出异常。

"use strict";

您还可以选择为特定功能启用严格模式,如下所示:

(function() {
    "use strict";
    // code here is in strict mode
})()

关于javascript - 在 IE11 中循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44390300/

相关文章:

javascript - 处理 HTML5 位置的最佳 JQuery 插件是什么?

javascript - 从具有相同类的字段中获取值,添加到数组

jquery - 运行 jQuery .cycle() 后,CSS 背景图像在 IE (6, 7, 8) 中消失

html - 输入类型 ="image"未在 IE 中加载

javascript - react : Warning: Failed prop type: Cannot read property 'apply' of undefined

javascript - 使用 AJAX 将变量发布到不同文件上的 php 函数并调用该函数

java - foreach 中的值输入字段到 foreach 外的输入字段

javascript - IE <选择> scrollTop

python - 如何使用 pywinauto 控制 IE 浏览器

jquery - 在 IE8(jQuery 广场)中的淡出/淡入效果期间不需要的透明背景