Reflect.get()
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.get() ã¯éçã¡ã½ããã§ãããããã£ã¢ã¯ã»ãµã¼æ§æã¨åæ§ã«å®è£
ããã¦ãã¾ããã颿°ã¨ãã¦åå¨ãã¾ãã
試ãã¦ã¿ã¾ããã
const object = {
x: 1,
y: 2,
};
console.log(Reflect.get(object, "x"));
// äºæ³ãããçµæ: 1
const array = ["zero", "one"];
console.log(Reflect.get(array, 1));
// äºæ³ãããçµæ: "one"
æ§æ
Reflect.get(target, propertyKey)
Reflect.get(target, propertyKey, receiver)
弿°
target-
ããããã£ãåå¾ãã対象ã®ãªãã¸ã§ã¯ãã
propertyKey-
è¨å®ããããããã£åã
receiverçç¥å¯-
ã²ãã¿ã¼ããã£ãå ´åã
targetã¸ã®å¼ã³åºãã§ä½¿ç¨ããthisã®å¤ãæä¾ãã¾ãã
è¿å¤
ããããã£ã®å¤ã§ãã
ä¾å¤
TypeError-
targetããªãã¸ã§ã¯ãã§ã¯ãªãã£ãå ´åã
解説
Reflect.get() ã¯ããããã£ã¢ã¯ã»ãµã¼ã®åå°çæå³ã¥ããæä¾ãã¾ããã¤ã¾ããReflect.get(target, propertyKey, receiver) ã¯æå³çã«æ¬¡ã®ãã®ã¨åçã§ãï¼
target[propertyKey];
é常ã®ããããã£ã¢ã¯ã»ã¹ã§ã¯ãtarget 㨠receiver ã¯æããã«åä¸ã®ãªãã¸ã§ã¯ãã§ãããã¨ã«æ³¨æãã¦ãã ããã
Reflect.get() ã¯ãtarget ã® [[Get]] ãªãã¸ã§ã¯ãå
é¨ã¡ã½ãã ãå¼ã³åºãã¾ãã
ä¾
>Reflect.get() ã®ä½¿ç¨
// ãªãã¸ã§ã¯ã
const obj1 = { x: 1, y: 2 };
Reflect.get(obj1, "x"); // 1
// é
å
Reflect.get(["zero", "one"], 1); // "one"
// åå¾ãã³ãã©ã¼ä»ããããã·ã¼
const obj2 = new Proxy(
{ p: 1 },
{
get(t, k, r) {
return `${k}bar`;
},
},
);
Reflect.get(obj2, "foo"); // "foobar"
// åå¾ãã³ãã©ã¼åã³ã¬ã·ã¼ãã¼ä»ããããã·ã¼
const obj3 = new Proxy(
{ p: 1, foo: 2 },
{
get(t, prop, receiver) {
return `${receiver[prop]}bar`;
},
},
);
Reflect.get(obj3, "foo", { foo: 3 }); // "3bar"
仿§æ¸
| Specification |
|---|
| ECMAScript® 2027 Language Specification> # sec-reflect.get> |