extends
Baseline
Widely available
This feature is well established and works across many devices and browser versions. Itâs been available across browsers since â¨2016ë 3ìâ©.
extends í¤ìëë í´ëì¤ë¥¼ ë¤ë¥¸ í´ëì¤ì ììì¼ë¡ ë§ë¤ê¸° ìí´ class ì ì¸ ëë class ìì ì¬ì©ë©ëë¤.
ìëí´ ë³´ê¸°
class DateFormatter extends Date {
getFormattedDate() {
const months = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
];
return `${this.getDate()}-${months[this.getMonth()]}-${this.getFullYear()}`;
}
}
console.log(new DateFormatter("August 19, 1975 23:15:30").getFormattedDate());
// Expected output: "19-Aug-1975"
구문
class ChildClass extends ParentClass { ... }
ì¤ëª
extends í¤ìëë ë´ì¥ ê°ì²´ë¿ë§ ìëë¼ ì¬ì©ì ì ì í´ëì¤ë¥¼ íì í´ëì¤ë¡ ë§ë¤ê¸° ìí´ ì¬ì©ë ì ììµëë¤.
íì¥( í´ëì¤)ì .prototypeì Object ëë nullì´ì´ì¼ í©ëë¤.
ì
>extends ì¬ì©í기
첫 ë²ì§¸ ìë Polygon í´ëì¤ë¡ë¶í° Square í´ëì¤ë¥¼ ë§ëëë¤. ì´ ìë live demo (source)ìì ë°ì·íìµëë¤.
class Square extends Polygon {
constructor(length) {
// ì¬ê¸°ì, lengthì í¨ê» ë¶ëª¨ í´ëì¤ì ìì±ì를 í¸ì¶
// Polygonì ëë¹ ë° ëì´ê° ì ê³µë¨
super(length, length);
// 주ì: íì í´ëì¤ìì, super()ê° ë¨¼ì í¸ì¶ëì´ì¼ 'this'를
// ì¬ì©í ì ììµëë¤. ì´ë¥¼ 빼먹ì¼ë©´ 참조 ì¤ë¥ê° ë°ìí©ëë¤.
this.name = "Square";
}
get area() {
return this.height * this.width;
}
set area(value) {
this.area = value;
}
}
ë´ì¥ ê°ì²´ì extends ì¬ì©í기
ì´ ìì ë ë´ì¥ ê°ì²´ Date를 íì¥í©ëë¤. ì´ ìì ë live demo (source)ìì ë°ì·íìµëë¤.
class myDate extends Date {
constructor() {
super();
}
getFormattedDate() {
var months = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
];
return (
this.getDate() + "-" + months[this.getMonth()] + "-" + this.getFullYear()
);
}
}
null íì¥
nullìì íì¥ì prototype ê°ì²´ê° Object.prototypeì¼ë¡ë¶í° ììë°ì§ ìì ê²ì ì ì¸íë©´ ë³´íµ í´ëì¤ì²ë¼ ëìí©ëë¤.
class nullExtends extends null {
constructor() {}
}
Object.getPrototypeOf(nullExtends); // Function.prototype
Object.getPrototypeOf(nullExtends.prototype); // null
ëª ì¸ì
| Specification |
|---|
| ECMAScript® 2026 Language Specification > # sec-class-definitions > |