Reflect.getOwnPropertyDescriptor()
Reflect
.getOwnPropertyDescriptor()
정적 메서드는 객체에 주어진 속성이 존재하면, 해당 속성의 서술자를 반환합니다. Object.getOwnPropertyDescriptor()
와 유사합니다.
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.
구문
Reflect.getOwnPropertyDescriptor(target, propertyKey)
매개변수
target
- 속성을 탐색할 객체.
propertyKey
- 자체 속성 서술자를 가져올 속성의 이름.
반환 값
대상 속성이 객체에 존재하면, 그 속성의 서술자. 존재하지 않으면 undefined
.
예제
설명
Reflect.getOwnPropertyDescriptor
메서드는 객체 속성의 서술자를 반환합니다. 만약 존재하지 않는 속성이라면 undefined
를 대신 반환합니다. 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()
의 첫 번째 매개변수가 객체가 아니고 원시값이라면 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 | Status | Comment |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Reflect.getOwnPropertyDescriptor' in that specification. |
Standard | Initial definition. |
ECMAScript (ECMA-262) The definition of 'Reflect.getOwnPropertyDescriptor' in that specification. |
Living Standard |
브라우저 호환성
BCD tables only load in the browser