handler.has()
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.has() ë©ìëë in ì°ì°ìì ëí í¸ë©ì
ëë¤.
ìëí´ ë³´ê¸°
const handler1 = {
has(target, key) {
if (key[0] === "_") {
return false;
}
return key in target;
},
};
const monster1 = {
_secret: "easily scared",
eyeCount: 4,
};
const proxy1 = new Proxy(monster1, handler1);
console.log("eyeCount" in proxy1);
// Expected output: true
console.log("_secret" in proxy1);
// Expected output: false
console.log("_secret" in monster1);
// Expected output: true
구문
js
new Proxy(target, {
has(target, prop) {},
});
ë§¤ê° ë³ì
ë¤ì 매ê°ë³ìë has() ë©ìëì ì ë¬ë©ëë¤. thisë ì²ë¦¬ê¸°ì ë°ì¸ë©ë©ëë¤.
ë°í ê°
has() ë©ìëë ë¶ë¦¬ì¸ ê°ì ë°íí©ëë¤.
ì¤ëª
handler.has() ë©ìëë in ì°ì°ìì ëí í¸ë©ì
ëë¤.
ê°ë¡ì±ê¸°
ì´ í¸ë©ì ë¤ì ìì ì ê°ë¡ì± ì ììµëë¤.
- ìì± ì¿¼ë¦¬:
foo in proxy - ììë ìì± ì¿¼ë¦¬:
foo in Object.create(proxy) withíì¸:with(proxy) { (foo); }Reflect.has()
ë¶ë³ ì¡°ê±´
ë¤ì ë¶ë³ ì¡°ê±´ì´ ìë°ëë©´ íë¡ììì TypeErrorê° ë°ìí©ëë¤.
- ìì±ì´ ëì ê°ì²´ì 구ì±í ì ìë ìì²´ ìì±ì¼ë¡ ì¡´ì¬íë ê²½ì°, ìì±ì´ ì¡´ì¬íì§ ìë ê²ì¼ë¡ ë³´ê³ ë ì ììµëë¤.
- ìì±ì´ ëì ê°ì²´ì ìì²´ ìì±ì¼ë¡ ì¡´ì¬íê³ ëì ê°ì²´ê° íì¥ë ì ìë ê²½ì°, ìì±ì´ ì¡´ì¬íì§ ìë ê²ì¼ë¡ ë³´ê³ ë ì ììµëë¤.
ìì
>in ì°ì°ì í¸ë©
ë¤ì ì½ëë in ì°ì°ì를 í¸ë©í©ëë¤.
js
const p = new Proxy(
{},
{
has(target, prop) {
console.log(`called: ${prop}`);
return true;
},
},
);
console.log("a" in p); // "called: a"
// true
ë¤ì ì½ëë ë¶ë³ ì¡°ê±´ì ìë°í©ëë¤.
js
const obj = { a: 10 };
Object.preventExtensions(obj);
const p = new Proxy(obj, {
has(target, prop) {
return false;
},
});
"a" in p; // TypeError is thrown
ëª ì¸ì
| Specification |
|---|
| ECMAScript® 2027 Language Specification> # sec-proxy-object-internal-methods-and-internal-slots-hasproperty-p> |
ë¸ë¼ì°ì í¸íì±
ê°ì´ 보기
ProxyProxy()ìì±ìinì°ì°ìReflect.has()