Reflect.getOwnPropertyDescriptor()
Baseline
Widely available
This feature is well established and works across many devices and browser versions. Itâs been available across browsers since 2016å¹´9æ.
Reflect.getOwnPropertyDescriptor() ã¯éçã¡ã½ããã§ãObject.getOwnPropertyDescriptor() ã¨ä¼¼ã¦ãã¾ãããªãã¸ã§ã¯ãã«æå®ãããããããã£ãåå¨ããå ´åããã®ããããã£è¨è¿°åãè¿ãã¾ããåå¨ããªãå ´å㯠undefined ãè¿ãã¾ãã
試ãã¦ã¿ã¾ããã
const object = {
property1: 42,
};
console.log(Reflect.getOwnPropertyDescriptor(object, "property1").value);
// äºæ³ãããçµæ: 42
console.log(Reflect.getOwnPropertyDescriptor(object, "property2"));
// äºæ³ãããçµæ: undefined
console.log(Reflect.getOwnPropertyDescriptor(object, "property1").writable);
// äºæ³ãããçµæ: true
æ§æ
Reflect.getOwnPropertyDescriptor(target, propertyKey)
弿°
target-
ããããã£ãæ¢ã対象ã®ãªãã¸ã§ã¯ãã
propertyKey-
ææãã¦ããããããã£è¨è¿°åãåå¾ããããã®ããããã£åã
è¿å¤
target ãªãã¸ã§ã¯ãå
ã«ããããã£ãåå¨ããå ´åã¯ãããããã£è¨è¿°åãªãã¸ã§ã¯ããã¾ã㯠undefinedã
ä¾å¤
TypeError-
targetããªãã¸ã§ã¯ãã§ã¯ãªãå ´å
解説
Reflect.getOwnPropertyDescriptor() ã¯ããªãã¸ã§ã¯ãã®ããããã£è¨è¿°åãåå¾ããåå°çæå³ã¥ããæä¾ãã¾ããObject.getOwnPropertyDescriptor() ã¨ã®å¯ä¸ã®éãã¯ããªãã¸ã§ã¯ã以å¤ã®å¯¾è±¡ã®å¦çæ¹æ³ã§ããReflect.getOwnPropertyDescriptor() ã¯å¯¾è±¡ããªãã¸ã§ã¯ãã§ãªãå ´åã« TypeError ãçºçãã¾ãããObject.getOwnPropertyDescriptor() ã¯ããããªãã¸ã§ã¯ãã«å¤æãã¾ãã
Reflect.getOwnPropertyDescriptor() ã¯ãtarget ã® [[GetOwnProperty]] ãªãã¸ã§ã¯ãå
é¨ã¡ã½ãã ãå¼ã³åºãã¾ãã
ä¾
>Reflect.getOwnPropertyDescriptor() ã®ä½¿ç¨
Reflect.getOwnPropertyDescriptor({ x: "hello" }, "x");
// {value: "hello", writable: true, enumerable: true, configurable: true}
Reflect.getOwnPropertyDescriptor({ x: "hello" }, "y");
// undefined
Reflect.getOwnPropertyDescriptor([], "length");
// {value: 0, writable: true, enumerable: false, configurable: false}
Object.getOwnPropertyDescriptor() ã¨ã®éã
ãã®ã¡ã½ããã¸ã®æåã®å¼æ°ããªãã¸ã§ã¯ãã§ã¯ãªã (ããªããã£ãã§ãã£ã) å ´åã TypeError ãçºçãã¾ãã Object.getOwnPropertyDescriptor ã ã¨ãéãªãã¸ã§ã¯ãã§ããæåã®å¼æ°ã¯å¼·å¶çã«ãªãã¸ã§ã¯ãã«å¤æããã¾ãã
Reflect.getOwnPropertyDescriptor("foo", 0);
// TypeError: "foo" is not non-null object
Object.getOwnPropertyDescriptor("foo", 0);
// { value: "f", writable: false, enumerable: true, configurable: false }
仿§æ¸
| Specification |
|---|
| ECMAScript® 2027 Language Specification> # sec-reflect.getownpropertydescriptor> |