handler.construct()
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.construct() ë©ìëë new ì°ì°ìì ëí í¸ë©ì
ëë¤. ê²°ê³¼ Proxy ê°ì²´ìì new ì°ì°ì´ ì í¨íë ¤ë©´ íë¡ì를 ì´ê¸°ííë ë° ì¬ì©ëë ëì ê°ì²´ ìì²´ì ë´ë¶ [[Construct]] ë©ìëê° ìì´ì¼ í©ëë¤(ì¦, new targetì´ ì í¨í´ì¼ í¨).
ìëí´ ë³´ê¸°
function monster1(disposition) {
this.disposition = disposition;
}
const handler1 = {
construct(target, args) {
console.log(`Creating a ${target.name}`);
// Expected output: "Creating a monster1"
return new target(...args);
},
};
const proxy1 = new Proxy(monster1, handler1);
console.log(new proxy1("fierce").disposition);
// Expected output: "fierce"
구문
new Proxy(target, {
construct(target, argumentsList, newTarget) {},
});
매ê°ë³ì
ë¤ì 매ê°ë³ìë construct() ë©ìëì ì ë¬ë©ëë¤. thisë ì²ë¦¬ê¸°ì ë°ì¸ë©ë©ëë¤.
target-
ëì ê°ì²´.
argumentsList-
ìì±ìì ì¸ì 목ë¡.
newTarget-
ìë í¸ì¶ë ìì±ì.
pì.
ë°í ê°
construct ë©ìëë ë°ëì ê°ì²´ë¥¼ ë°íí©ëë¤.
ì¤ëª
handler.construct() ë©ìëë new ì°ì°ìì ëí í¸ë©ì
ëë¤.
ê°ë¡ì±ê¸°
ì´ í¸ë©ì ë¤ì ìì ì ê°ë¡ì± ì ììµëë¤.
new myFunction(...args)Reflect.construct()
ë¶ë³ ì¡°ê±´
ë¤ì ë¶ë³ ì¡°ê±´ì´ ìë°ëë©´ íë¡ììì TypeErrorê° ë°ìí©ëë¤.
- ê²°ê³¼ë
Objectì¬ì¼ í©ëë¤.
ìì
>new ì°ì°ì í¸ë©í기
ë¤ì ì½ëë new ì°ì°ì를 í¸ë©í©ëë¤.
const p = new Proxy(function () {}, {
construct(target, argumentsList, newTarget) {
console.log(`called: ${argumentsList}`);
return { value: argumentsList[0] * 10 };
},
});
console.log(new p(1).value); // "í¸ì¶: 1"
// 10
ë¤ì ì½ëë ë¶ë³ ì¡°ê±´ì ìë°í©ëë¤.
const p = new Proxy(function () {}, {
construct(target, argumentsList, newTarget) {
return 1;
},
});
new p(); // TypeError ìì¸ ë°ì
ë¤ì ì½ëë íë¡ì를 ë¶ì ì íê² ì´ê¸°íí©ëë¤. íë¡ì ì´ê¸°íì target ìì²´ë new ì°ì°ì ëí ì í¨í ìì±ìì¬ì¼ í©ëë¤.
const p = new Proxy(
{},
{
construct(target, argumentsList, newTarget) {
return {};
},
},
);
new p(); // TypeError is thrown, "p" is not a constructor
ëª ì¸ì
| Specification |
|---|
| ECMAScript® 2027 Language Specification> # sec-proxy-object-internal-methods-and-internal-slots-construct-argumentslist-newtarget> |
ë¸ë¼ì°ì í¸íì±
ê°ì´ 보기
ProxyProxy()ìì±ìnewì°ì°ì.Reflect.construct()