Object.prototype.propertyIsEnumerable()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. Itâs been available across browsers since иÑÐ»Ñ 2015 г..
Сводка
ÐеÑод propertyIsEnumerable() возвÑаÑÐ°ÐµÑ Ð»Ð¾Ð³Ð¸ÑеÑкое знаÑение, ÑказÑваÑÑее, ÑвлÑеÑÑÑ Ð»Ð¸ Ñказанное ÑвойÑÑво пеÑеÑиÑлÑемÑм.
СинÑакÑиÑ
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
СпеÑиÑикаÑии
| Specification |
|---|
| ECMAScript® 2027 Language Specification> # sec-object.prototype.propertyisenumerable> |