Baseline Widely available
new.target
ã¯æ¬ä¼¼ããããã£ã§ã颿°ãã³ã³ã¹ãã©ã¯ã¿ã¼ã new æ¼ç®åã使ç¨ãã¦å¼ã³åºããããã©ãããæ¤åºãããã¨ãã§ãã¾ãã new æ¼ç®åã使ç¨ãã¦å¼ã³åºãããã³ã³ã¹ãã©ã¯ã¿ã¼ã颿°ã®ä¸ã§ã¯ã new.target
ã¯ã³ã³ã¹ãã©ã¯ã¿ã¼ã颿°ã¸ã®åç
§ãè¿ãã¾ããé常ã®é¢æ°å¼ã³åºãã®å ´åã new.target
㯠undefined
ã«ãªãã¾ãã
function Foo() {
if (!new.target) {
throw new TypeError("calling Foo constructor without new is invalid");
}
}
try {
Foo();
} catch (e) {
console.log(e);
// Expected output: TypeError: calling Foo constructor without new is invalid
}
æ§æ 解説
new.target
ã®æ§æã¯ãnew
ãã¼ã¯ã¼ãã¨ãããã target
èå¥åã§æ§æããã¦ãã¾ããé常ããããã®å·¦å´ã¯ããããã£ã¢ã¯ã»ã¹ãè¡ããããªãã¸ã§ã¯ãã§ãããããã§ã® new
ã¯ãªãã¸ã§ã¯ãã§ã¯ããã¾ããã
new.target
ã¯ããã¹ã¦ã®é¢æ°ã§ä½¿ç¨ã§ããæ¬ä¼¼ããããã£ã§ãã
ã¯ã©ã¹ã®ã³ã³ã¹ãã©ã¯ã¿ã¼ã§ã¯ãæ§ç¯ãããã¯ã©ã¹ãåç §ãã¾ãã
é常ã®é¢æ°ã§ã¯ãnew æ¼ç®åãä»ãã¦å¼ã³åºãããã¨ä»®å®ãã¦ã颿°èªä½ãåç
§ãã¾ãããã以å¤ã®å ´åãnew.target
㯠undefined
ã«ãªãã¾ãã
ã¢ãã¼é¢æ°ã§ã¯ãnew.target
ã¯å¨å²ã®ã¹ã³ã¼ãããç¶æ¿ããã¾ãã
é常ã®é¢æ°å¼ã³åºãã§ã¯ (ã³ã³ã¹ãã©ã¯ã¿ã¼é¢æ°ã®å¼ã³åºãã¨ã¯å¯¾ç
§çã«)ã new.target
㯠undefined
ã«ãªãã¾ããããã«ããã颿°ã new ã§ã³ã³ã¹ãã©ã¯ã¿ã¼ã¨ãã¦å¼ã³åºãããããæ¤åºã§ãã¾ãã
function Foo() {
if (!new.target) {
throw "Foo() must be called with new";
}
console.log("Foo instantiated with new");
}
new Foo(); // "Foo instantiated with new" ãåºå
Foo(); // "Foo() must be called with new" ä¾å¤ãçºç
ã³ã³ã¹ãã©ã¯ã¿ã¼ã«ããã new.target
ã¯ã©ã¹ã®ã³ã³ã¹ãã©ã¯ã¿ã¼ã§ã¯ãnew.target
㯠new
ã§ç´æ¥å®è¡ãããã³ã³ã¹ãã©ã¯ã¿ã¼ãåç
§ãã¾ããããã¯ãã³ã³ã¹ãã©ã¯ã¿ã¼ã親ã¯ã©ã¹ã«ãããåã³ã³ã¹ãã©ã¯ã¿ã¼ããå§ä»»ãããå ´åãåæ§ã§ãã
class A {
constructor() {
console.log(new.target.name);
}
}
class B extends A {
constructor() {
super();
}
}
let a = new A(); // logs "A"
let b = new B(); // logs "B"
class C {
constructor() {
console.log(new.target);
}
}
class D extends C {
constructor() {
super();
}
}
let c = new C(); // logs class C{constructor(){console.log(new.target);}}
let d = new D(); // logs class D extends C{constructor(){super();}}
ä¸è¨ã® C
ããã³ D
ã¯ã©ã¹ã®ä¾ããã new.target
ã¯åæåãããã¯ã©ã¹ã®ã¯ã©ã¹å®ç¾©ãæãã¦ããããã«è¦ãã¾ãããã¨ãã°ãd
ã new D()
ã§åæåããå ´åã¯ã D
ã®ã¯ã©ã¹å®ç¾©ãåºåãããåæ§ã« c
ã®å ´å㯠C
ã®ã¯ã©ã¹ãåºåããã¾ãã
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4