Javascript 在这个for/of循环中,什么';它相当于传统的IE11 for循环?

Javascript 在这个for/of循环中,什么';它相当于传统的IE11 for循环?,javascript,internet-explorer-11,Javascript,Internet Explorer 11,我试图使下面的代码与IE11兼容,但我仍停留在for/of循环行(第10行) 我相信mutationsList是一个合适的节点列表(但我可能错了)。关于这一点,我如何将for(let mutations list的变异){转换为传统的for循环?作为一个只懂基本JavaScript的人,您是否也可以将for(let mutations list的变异){转换为更简单的术语 for(let mutation of mutationsList) { 翻译 for(var mutation = 0

我试图使下面的代码与IE11兼容,但我仍停留在for/of循环行(第10行)


我相信mutationsList是一个合适的节点列表(但我可能错了)。关于这一点,我如何将
for(let mutations list的变异){
转换为传统的for循环?作为一个只懂基本JavaScript的人,您是否也可以将
for(let mutations list的变异){
转换为更简单的术语

 for(let mutation of mutationsList) {
翻译

for(var mutation = 0; mutation < mutationsList.length; mutation++){
   var something = mutationsList[mutation] ; // access the element
}
for(var-mutation=0;mutation
循环使用枚举上存在的迭代器。这意味着迭代器的行为将完全被传统方法回避。也就是说,在许多情况下,您处理的是可以使用整数索引枚举的数组或类似数组的对象

对于这些简单的情况,它们大致可以转换为:

for(设[1,2,3]中的x){
console.log(x)
}
大致相当于:

for(var _i=0,_arr=[1,2,3];_i<_arr.length;_i++){
变量x=_arr[_i];
控制台日志(x);
}
在您的代码中,
for…of
用于枚举传递给
MutationObserver
回调的
MutationRecord
对象数组。
for…of
在数组上工作,因为
array
具有
符号。迭代器
在其原型上。普通的for循环也可以工作,因为数组有元素具有与整数对应的键的ts,可使用(例如,
arr[0]
)访问。

传统for循环:

for(var i=0; i<mutationsList.length; i++) {
    var mutation = mutationsList[i];
    if (mutation.type === 'childList') {
        console.log('A child node has been added or removed.');
    }
    else if (mutation.type === 'attributes') {
        console.log('The ' + mutation.attributeName + ' attribute was modified.');
    }
}
for(var i=0;i不支持IE浏览器,因此,您可以使用或循环浏览列表,它们支持IE浏览器

请尝试按以下方式修改您的代码:

for(let mutation in mutationsList) {
    if (mutation.type === 'childList') {
        console.log('A child node has been added or removed.');
    }
    else if (mutation.type === 'attributes') {
        console.log('The ' + mutation.attributeName + ' attribute was modified.');
    }
}

for(设x/y)
=
for(var i=0;i
使用
mutationsList++
应该是
mutation++
for(let mutation in mutationsList) {
    if (mutation.type === 'childList') {
        console.log('A child node has been added or removed.');
    }
    else if (mutation.type === 'attributes') {
        console.log('The ' + mutation.attributeName + ' attribute was modified.');
    }
}