Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 有人能给我解释一下for-in循环在这里是如何被调用3次的吗?_Javascript_Loops_For In Loop - Fatal编程技术网

Javascript 有人能给我解释一下for-in循环在这里是如何被调用3次的吗?

Javascript 有人能给我解释一下for-in循环在这里是如何被调用3次的吗?,javascript,loops,for-in-loop,Javascript,Loops,For In Loop,代码如下: var InvertedPeninsula = function() { this.inhabitants = [ { name: 'Sir Charles', race: 'Human' }, { name: 'Ealei', race: 'Elf' } ]; // Adds an extra humans method property to the inhabitants array t

代码如下:

var InvertedPeninsula = function() {
  this.inhabitants = [
    {
      name: 'Sir Charles',
      race: 'Human'
    },
    {
      name: 'Ealei',
      race: 'Elf'
    }
  ];
  // Adds an extra humans method property to the inhabitants array to return all Humans
  this.inhabitants.humans = function() { /* returns all Human inhabitants */ };
};

// Create a new invertedPeninsula
var invertedPeninsula = new InvertedPeninsula();

// Log the name of each invertedPeninsula inhabitant
for (var i in invertedPeninsula.inhabitants) {
  console.log(invertedPeninsula.inhabitants[i].name);
}

对我来说,它看起来像是2x。3x从哪里来?数组中只有2个单元格。

这正是使用
for…in
迭代数组时遇到的陷阱。
for…in
会迭代所有可枚举属性,因此
humans
也会被迭代

数组中当然有两个对象,但肯定有三个属性:
0
1
人类
,因此有三个属性

invertedPeninsula.inhabitants.forEach(function(habitat){
   console.log(habitat.name);
});

Array.forEach
是您的最佳选择,因为它迭代编号的属性。正常的
for
循环也可以。

您的数组具有以下值:
0,1,'humans'
事实上,您使用以下代码添加了名为'humans'的元素:

this.inhabitants.humans = function() { /* returns all Human inhabitants */ };
要检查它,请尝试以下代码:

var InvertedPeninsula = function() {
  this.inhabitants = [
    {
      name: 'Sir Charles',
      race: 'Human'
    },
    {
      name: 'Ealei',
      race: 'Elf'
    }
  ];
  // Adds an extra humans method property to the inhabitants array to return all Humans
  this.inhabitants.humans = function() { /* returns all Human inhabitants */ };
};

// Create a new invertedPeninsula
var invertedPeninsula = new InvertedPeninsula();

// Log the name of each invertedPeninsula inhabitant
for (var i in invertedPeninsula.inhabitants) {
  console.log(i);   // CHANGED LINE
}

for…in
迭代所有(继承的和自己的)可枚举属性

使用赋值创建属性时,该属性定义为可枚举。您可以使用

Object.defineProperty(this.people,'humans',{enumerable:false});
var InvertedPeninsula=函数(){
此参数=[
{
姓名:'查尔斯爵士',
种族:“人类”
},
{
名称:“Ealei”,
种族:“精灵”
}
];
//将一个额外的humans方法属性添加到includes数组以返回所有人
this.incidents.humans=function(){/*返回所有人类居民*/};
Object.defineProperty(this.people,'humans',{enumerable:false});
};
//创建一个新的invertedPeninsula
var invertedPeninsula=新invertedPeninsula();
//记录每个居住者的姓名
适用于(invertedPeninsula.居民中的一号变量){
console.log(invertedPeninsula.consolids[i].name);

}
如果检查
i
的值,所有内容都将变得清晰。请参见(不确定是否重复):如果要迭代驻留,请使用正确的循环。@Bergi Right,我没有看到它是数组。