--- title: Reflect.getOwnPropertyDescriptor() slug: Web/JavaScript/Reference/Global_Objects/Reflect/getOwnPropertyDescriptor tags: - ECMAScript 2015 - JavaScript - Method - Reference - Reflect translation_of: Web/JavaScript/Reference/Global_Objects/Reflect/getOwnPropertyDescriptor ---
Reflect
.getOwnPropertyDescriptor()
정적 메서드는 객체에 주어진 속성이 존재하면, 해당 속성의 서술자를 반환합니다. {{jsxref("Object.getOwnPropertyDescriptor()")}}와 유사합니다.
Reflect.getOwnPropertyDescriptor(target, propertyKey)
target
propertyKey
대상 속성이 객체에 존재하면, 그 속성의 서술자. 존재하지 않으면 {{jsxref("undefined")}}.
target
이 {{jsxref("Object")}}가 아니면 {{jsxref("TypeError")}}.
Reflect.getOwnPropertyDescriptor
메서드는 객체 속성의 서술자를 반환합니다. 만약 존재하지 않는 속성이라면 {{jsxref("undefined")}}를 대신 반환합니다. {{jsxref("Object.getOwnPropertyDescriptor()")}}와의 유일한 차이는 객체가 아닌 대상의 처리 방법입니다.
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()
와의 차이점Reflect.getOwnPropertyDescriptor()
의 첫 번째 매개변수가 객체가 아니고 {{glossary("Primitive", "원시값")}}이라면 {{jsxref("TypeError")}}가 발생합니다. 반면 {{jsxref("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 | Status | Comment |
---|---|---|
{{SpecName('ES2015', '#sec-reflect.getownpropertydescriptor', 'Reflect.getOwnPropertyDescriptor')}} | {{Spec2('ES2015')}} | Initial definition. |
{{SpecName('ESDraft', '#sec-reflect.getownpropertydescriptor', 'Reflect.getOwnPropertyDescriptor')}} | {{Spec2('ESDraft')}} |
{{Compat("javascript.builtins.Reflect.getOwnPropertyDescriptor")}}