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()
์ ์ ์ฌํฉ๋๋ค.
์๋ํด ๋ณด๊ธฐ
const object1 = {
property1: 42,
};
console.log(Reflect.getOwnPropertyDescriptor(object1, "property1").value);
// Expected output: 42
console.log(Reflect.getOwnPropertyDescriptor(object1, "property2"));
// Expected output: undefined
console.log(Reflect.getOwnPropertyDescriptor(object1, "property1").writable);
// Expected output: true
๊ตฌ๋ฌธ
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 |
---|
ECMAScriptยฎ 2026 Language Specification # sec-reflect.getownpropertydescriptor |
๋ธ๋ผ์ฐ์ ํธํ์ฑ
Loadingโฆ