handler.set()
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ì.
handler.set() ë©ìëë ìì± ê°ì ì¤ì ì ìí í¸ë©ì
ëë¤.
ìëí´ ë³´ê¸°
const monster1 = { eyeCount: 4 };
const handler1 = {
set(obj, prop, value) {
if (prop === "eyeCount" && value % 2 !== 0) {
console.log("Monsters must have an even number of eyes");
} else {
return Reflect.set(...arguments);
}
},
};
const proxy1 = new Proxy(monster1, handler1);
proxy1.eyeCount = 1;
// Expected output: "Monsters must have an even number of eyes"
console.log(proxy1.eyeCount);
// Expected output: 4
proxy1.eyeCount = 2;
console.log(proxy1.eyeCount);
// Expected output: 2
구문
new Proxy(target, {
set(target, property, value, receiver) {},
});
ë§¤ê° ë³ì
ë¤ì 매ê°ë³ìë set() ë©ìëì ì ë¬ë©ëë¤. thisë ì²ë¦¬ê¸°ì ë°ì¸ë©ë©ëë¤.
target-
ëì ê°ì²´
property-
ì¤ì í ìì±ì ì´ë¦ ëë
Symbol value-
ì¤ì í ìì±ì ì ê°
receiver-
í ë¹ì´ ì§ìë ìë ê°ì²´ì ëë¤. ì´ê²ì ì¼ë°ì ì¼ë¡ íë¡ì ìì²´ì ëë¤. ê·¸ë¬ë
set()ì²ë¦¬ê¸°ë íë¡í íì ì²´ì¸ì´ë ë¤ìí ë¤ë¥¸ ë°©ë² ë±ì íµí´ ê°ì ì ì¼ë¡ í¸ì¶í ìë ììµëë¤.ì를 ë¤ì´, ì¤í¬ë¦½í¸ê°
obj.name = "jen"ì ìííëë°,objë íë¡ìê° ìëë©´ì ìì±.nameì´ ìê³ , íë¡í íì ì²´ì¸ìë íë¡ìê° ìë¤ê³ ê°ì í´ë´ ìë¤. ì´ë í´ë¹ íë¡ììset()ì²ë¦¬ê¸°ê° í¸ì¶ëê³ ëìobjê° ìì ìë¡ ì ë¬ë©ëë¤.
ë°í ê°
set() ë©ìëë ë¶ë¦¬ì¸ ê°ì ë°íí©ëë¤.
- í ë¹ì´ ì±ê³µíì¼ë©´
true를 ë°íí©ëë¤. - ì격 모ëìì
set()ë©ìëê°false를 ë°ííë©´TypeErrorìì¸ê° ë°ìí©ëë¤.
ì¤ëª
handler.set() ë©ìëë ìì± ê°ì ì¤ì ì ìí í¸ë©ì
ëë¤.
ê°ë¡ì±ê¸°
ì´ í¸ë©ì ë¤ì ìì ì ê°ë¡ì± ì ììµëë¤.
- ìì± í ë¹:
proxy[foo] = barìproxy.foo = bar - ììë ìì± í ë¹:
Object.create(proxy)[foo] = bar Reflect.set()
ë¶ë³ ì¡°ê±´
ë¤ì ë¶ë³ ì¡°ê±´ì´ ìë°ëë©´ íë¡ììì TypeErrorê° ë°ìí©ëë¤.
- ëì ê°ì²´ ìì±ì´ ì°ê¸° ë° êµ¬ì±í ì ìë ë°ì´í° ìì±ì¸ ê²½ì°, ìì± ê°ì ëì ê°ì²´ ìì±ì ê°ê³¼ ë¤ë¥´ê² ë³ê²½í ì ììµëë¤.
- ëì ê°ì²´ ìì±ì
[[Set]]ìì±ì´undefinedì¸ êµ¬ì± ë¶ê°ë¥í ì ê·¼ì ìì±ì¸ ê²½ì°, ìì± ê°ì ì¤ì í ì ììµëë¤. - ì격 모ëìì
set()ì²ë¦¬ê¸°ê°false를 ë°ííë©´TypeErrorìì¸ê° ë°ìí©ëë¤.
ìì
>ìì± ê° ì¤ì í¸ë©
ë¤ì ì½ëë ìì± ê°ì ì¤ì íë ê²ì í¸ë©í©ëë¤.
const p = new Proxy(
{},
{
set(target, prop, value, receiver) {
target[prop] = value;
console.log(`property set: ${prop} = ${value}`);
return true;
},
},
);
console.log("a" in p); // false
p.a = 10; // "property set: a = 10"
console.log("a" in p); // true
console.log(p.a); // 10
ëª ì¸ì
| Specification |
|---|
| ECMAScript® 2027 Language Specification > # sec-proxy-object-internal-methods-and-internal-slots-set-p-v-receiver > |