Метод propertyIsEnumerable() повертає булеве значення, що позначає, чи є вказана властивість перелічуваною.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Синтаксис
obj.propertyIsEnumerable(prop)
Параметри
prop- Ім'я властивості, яку необхідно перевірити.
Значення, що повертається
Булеве значення, що позначає, чи є вказана властивість перелічуваною.
Опис
Кожен об'єкт має метод propertyIsEnumerable. Цей метод може визначити, чи може вказана властивість об'єкта бути перелічена циклом for...in, за винятком властивостей, успадкованих через ланцюжок прототипів. Якщо об'єкт не має вказаної властивості, метод поверне false.
Приклади
Базове використання propertyIsEnumerable
Наступний приклад демонструє використання propertyIsEnumerable на об'єктах та масивах:
var o = {};
var a = [];
o.prop = 'перелічувана';
a[0] = 'перелічувана';
o.propertyIsEnumerable('prop'); // повертає true
a.propertyIsEnumerable(0); // повертає true
Визначені користувачем об'єкти проти вбудованих об'єктів
Наступний приклад демонструє перелічуваність визначених користувачем властивостей у порівнянні з вбудованими:
var a = ['перелічувана'];
a.propertyIsEnumerable(0); // повертає true
a.propertyIsEnumerable('length'); // повертає false
Math.propertyIsEnumerable('random'); // повертає false
this.propertyIsEnumerable('Math'); // повертає false
Прямі властивості проти успадкованих
var a = [];
a.propertyIsEnumerable('constructor'); // повертає false
function firstConstructor() {
this.property = 'не перелічувана';
}
firstConstructor.prototype.firstMethod = function() {};
function secondConstructor() {
this.method = function method() { return 'перелічувана'; };
}
secondConstructor.prototype = new firstConstructor;
secondConstructor.prototype.constructor = secondConstructor;
var o = new secondConstructor();
o.arbitraryProperty = 'перелічувана';
o.propertyIsEnumerable('arbitraryProperty'); // повертає true
o.propertyIsEnumerable('method'); // повертає true
o.propertyIsEnumerable('property'); // повертає false
o.property = 'перелічувана';
o.propertyIsEnumerable('property'); // повертає true
// Наступні повертають false, оскільки вони присутні на прототипі, який
// propertyIsEnumerable не враховує (хоча останні дві
// перебираються циклом for-in)
o.propertyIsEnumerable('prototype'); // повертає false (згідно з JS 1.8.1/FF3.6)
o.propertyIsEnumerable('constructor'); // повертає false
o.propertyIsEnumerable('firstMethod'); // повертає false
Специфікації
| Специфікація | Статус | Коментар |
|---|---|---|
| ECMAScript 3rd Edition (ECMA-262) | Standard | Початкове визначення |
| ECMAScript 5.1 (ECMA-262) The definition of 'Object.prototype.propertyIsEnumerable' in that specification. |
Standard | |
| ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Object.prototype.propertyIsEnumerable' in that specification. |
Standard | |
| ECMAScript (ECMA-262) The definition of 'Object.prototype.propertyIsEnumerable' in that specification. |
Living Standard |
Сумісність з веб-переглядачами
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
propertyIsEnumerable | Chrome Full support 1 | Edge Full support 12 | Firefox Full support 1 | IE Full support 5.5 | Opera Full support 4 | Safari Full support 3 | WebView Android Full support 1 | Chrome Android Full support 18 | Firefox Android Full support 4 | Opera Android Full support 10.1 | Safari iOS Full support 1 | Samsung Internet Android Full support 1.0 | nodejs Full support Yes |
Legend
- Full support
- Full support