在forin循环中只显示自定义对象属性。

时间:2021-05-01 05:54:35

Array.prototype.myFeature = function() {};

var arr = ['some', 'items'];

for (var prop in arr) {
  console.log(prop);
}

The output for that code will be: 0, 1, myFeature.

该代码的输出将是:0,1,myFeature。

The question is: why only custom added function to the Array prototype is outputed, instead of all the function that exists in prototype?

问题是:为什么只输出数组原型的自定义添加函数,而不输出原型中存在的所有函数?

2 个解决方案

#1


18  

This is because built-in array methods are defined to be non-enumerable, while properties created by ordinary assignment are enumerable. Internally, properties have behavioral features specified by their associated property descriptor which is defined at property-creation time. One feature supplied in this way is the property's enumerability.

这是因为内置数组方法被定义为不可枚举的,而普通赋值创建的属性是可枚举的。在内部,属性具有由相关属性描述符指定的行为特性,这些属性描述符在属性创建时定义。以这种方式提供的一个特性是属性的可枚举性。

Compare

比较

> Object.getOwnPropertyDescriptor(Array.prototype, "join")
{value: ƒ, writable: true, enumerable: false, configurable: true}

and

> Object.getOwnPropertyDescriptor(Array.prototype, "myFeature")
{value: ƒ, writable: true, enumerable: true, configurable: true}

The first property descriptor object has enumerable: false while the second has enumerable: true. Non-enumerable properties are not enumerated in for-in loops.

第一个属性描述符对象是可枚举的:false,而第二个属性是可枚举的:true。不可枚举的属性在forin循环中没有被枚举。

You can define your own non-enumerable property with Object.defineProperty:

您可以使用Object.defineProperty定义自己的不可枚举属性:

Object.defineProperty(Array.prototype, "myFeature", { value: function() {}, enumerable: false });

#2


4  

Per the docs:

每个文档:

The for...in statement iterates over the enumerable properties of an object. For each distinct property, statements can be executed.

为…在语句中迭代对象的可枚举属性。对于每个不同的属性,可以执行语句。

Seems, only these three properties are enumerable. So for...in only iterates over them.

似乎,只有这三个属性是可枚举的。所以对于…只对它们进行迭代。

#1


18  

This is because built-in array methods are defined to be non-enumerable, while properties created by ordinary assignment are enumerable. Internally, properties have behavioral features specified by their associated property descriptor which is defined at property-creation time. One feature supplied in this way is the property's enumerability.

这是因为内置数组方法被定义为不可枚举的,而普通赋值创建的属性是可枚举的。在内部,属性具有由相关属性描述符指定的行为特性,这些属性描述符在属性创建时定义。以这种方式提供的一个特性是属性的可枚举性。

Compare

比较

> Object.getOwnPropertyDescriptor(Array.prototype, "join")
{value: ƒ, writable: true, enumerable: false, configurable: true}

and

> Object.getOwnPropertyDescriptor(Array.prototype, "myFeature")
{value: ƒ, writable: true, enumerable: true, configurable: true}

The first property descriptor object has enumerable: false while the second has enumerable: true. Non-enumerable properties are not enumerated in for-in loops.

第一个属性描述符对象是可枚举的:false,而第二个属性是可枚举的:true。不可枚举的属性在forin循环中没有被枚举。

You can define your own non-enumerable property with Object.defineProperty:

您可以使用Object.defineProperty定义自己的不可枚举属性:

Object.defineProperty(Array.prototype, "myFeature", { value: function() {}, enumerable: false });

#2


4  

Per the docs:

每个文档:

The for...in statement iterates over the enumerable properties of an object. For each distinct property, statements can be executed.

为…在语句中迭代对象的可枚举属性。对于每个不同的属性,可以执行语句。

Seems, only these three properties are enumerable. So for...in only iterates over them.

似乎,只有这三个属性是可枚举的。所以对于…只对它们进行迭代。