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å¹´7æ.
propertyIsEnumerable() ã¡ã½ããã¯ãæå®ãããããããã£ãåæå¯è½ã§ããã¤ãªãã¸ã§ã¯ãèªèº«ã®ããããã£ã§ãããã©ããã示ãè«çå¤ãè¿ãã¾ãã
試ãã¦ã¿ã¾ããã
const object = {};
const array = [];
object.foo = 42;
array[0] = 42;
console.log(object.propertyIsEnumerable("foo"));
// äºæ³ãããçµæ: true
console.log(array.propertyIsEnumerable(0));
// äºæ³ãããçµæ: true
console.log(array.propertyIsEnumerable("length"));
// äºæ³ãããçµæ: false
æ§æ
propertyIsEnumerable(prop)
弿°
è¿å¤
è«çå¤ã§ãæå®ãããããããã£ãåæå¯è½ã§ããããã¤ãªãã¸ã§ã¯ãèªèº«ã®ããããã£ã§ãããã©ããã示ãã¾ãã
解説
ãã¹ã¦ã®ãªãã¸ã§ã¯ã㯠Object.prototype ããï¼ã¤ã¾ãã null ãããã¿ã¤ããªãã¸ã§ã¯ããé¤ããã¹ã¦ãï¼ propertyIsEnumerable ã¡ã½ãããç¶æ¿ãã¦ãã¾ãããã®ã¡ã½ããã¯ãæå®ããããããã£ï¼æååã¾ãã¯ã·ã³ãã«ï¼ããªãã¸ã§ã¯ãã®åæå¯è½ãªèªåèªèº«ã®ããããã£ã§ãããã©ãããå¤å®ãã¾ãããªãã¸ã§ã¯ããæå®ããããããã£ãæã£ã¦ããªãå ´åããã®ã¡ã½ãã㯠false ãè¿ãã¾ãã
ãã®ã¡ã½ãã㯠Object.getOwnPropertyDescriptor(obj, prop)?.enumerable ?? false ã¨ç価ã§ãã
ä¾
>propertyIsEnumerable() ã®åºæ¬çãªä½¿ãæ¹
以ä¸ã®ä¾ã¯ãªãã¸ã§ã¯ãã¨é
åã§ã® propertyIsEnumerable() ã®ä½¿ãæ¹ã示ãã¦ãã¾ãã
const o = {};
const a = [];
o.prop = "is enumerable";
a[0] = "is enumerable";
o.propertyIsEnumerable("prop"); // true
a.propertyIsEnumerable(0); // true
ã¦ã¼ã¶ã¼å®ç¾©ãªãã¸ã§ã¯ãã¨çµã¿è¾¼ã¿ãªãã¸ã§ã¯ã
以ä¸ã®ä¾ã¯ãã¦ã¼ã¶ã¼å®ç¾©ããããã£ã¨çµã¿è¾¼ã¿ããããã£ã®åæå¯è½æ§ãå®è¨¼ãã¦ãã¾ãã
const a = ["is enumerable"];
a.propertyIsEnumerable(0); // true
a.propertyIsEnumerable("length"); // false
Math.propertyIsEnumerable("random"); // false
globalThis.propertyIsEnumerable("Math"); // false
ç´æ¥ã®ããããã£ã¨ç¶æ¿ãããããããã£
åæå¯è½ãªèªåèªèº«ã§æã¤ããããã£ã ãã propertyIsEnumerable() ã§ true ãè¿ãã¾ãããç¶æ¿ããããã®ãå«ããã¹ã¦ã®åæå¯è½ãªããããã£ã¯ for...in ã«ã¼ãã«ãã£ã¦å¦çããã¾ãã
const o1 = {
enumerableInherited: "is enumerable",
};
Object.defineProperty(o1, "nonEnumerableInherited", {
value: "is non-enumerable",
enumerable: false,
});
const o2 = {
// o1 㯠o2 ã®ãããã¿ã¤ã
__proto__: o1,
enumerableOwn: "is enumerable",
};
Object.defineProperty(o2, "nonEnumerableOwn", {
value: "is non-enumerable",
enumerable: false,
});
o2.propertyIsEnumerable("enumerableInherited"); // false
o2.propertyIsEnumerable("nonEnumerableInherited"); // false
o2.propertyIsEnumerable("enumerableOwn"); // true
o2.propertyIsEnumerable("nonEnumerableOwn"); // false
ã·ã³ãã«ããããã£ã®æ¤æ»
propertyIsEnumerable() ã¯ã·ã³ãã«ããããã£ã«ã対å¿ãã¦ãã¾ãããªããå¤ãã®åæã¡ã½ããã¯ãæååããããã£ã®ã¿ãæ±ãã¾ããã·ã³ãã«ããããã£ã®åæå¯è½æ§ã¯ãObject.assign() ãã¹ãã¬ããæ§æã使ç¨ãã¦ããå ´åã«ã®ã¿æçã§ãã詳細ã«ã¤ãã¦ã¯ãããããã£ã®åæå¯è½æ§ã¨æææ¨©ãåç
§ãã¦ãã ããã
const sym = Symbol("enumerable");
const sym2 = Symbol("non-enumerable");
const o = {
[sym]: "is enumerable",
};
Object.defineProperty(o, sym2, {
value: "is non-enumerable",
enumerable: false,
});
o.propertyIsEnumerable(sym); // true
o.propertyIsEnumerable(sym2); // false
null ããããã£ãªãã¸ã§ã¯ãã®ä½¿ç¨
null ãããã¿ã¤ããªãã¸ã§ã¯ã㯠Object.prototype ãç¶æ¿ãã¦ããªãããã propertyIsEnumerable() ã¡ã½ãããç¶æ¿ãã¾ããã代ããã«ãªãã¸ã§ã¯ãã this ã¨ã㦠Object.prototype.propertyIsEnumerable ãå¼ã³åºãå¿
è¦ãããã¾ãã
const o = {
__proto__: null,
enumerableOwn: "is enumerable",
};
o.propertyIsEnumerable("enumerableOwn"); // TypeError: o.propertyIsEnumerable ã¯é¢æ°ã§ã¯ããã¾ãã
Object.prototype.propertyIsEnumerable.call(o, "enumerableOwn"); // true
ã¾ãã代ããã« Object.getOwnPropertyDescriptor() ã使ç¨ãããã¨ãã§ãã¾ããããã¯ãåå¨ããªãããããã£ã¨å®éã«åæã§ããªãããããã£ãå¤å¥ããã®ã«ãæçã§ãã
const o = {
__proto__: null,
enumerableOwn: "is enumerable",
};
Object.getOwnPropertyDescriptor(o, "enumerableOwn")?.enumerable; // true
Object.getOwnPropertyDescriptor(o, "nonExistent")?.enumerable; // undefined
仿§æ¸
| Specification |
|---|
| ECMAScript® 2027 Language Specification> # sec-object.prototype.propertyisenumerable> |