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
้้ตๅญๅฏ็จๆผๅปบ็ซไธๅ่ช่จ้กๅฅๆๅ
งๅปบ้กๅฅ็ๅญ้กๅฅใ
็ฏไพ
ไฝฟ็จ extends
็ฌฌไธๅ็ฏไพๆฏๆ นๆ Polygon
้กๅฅๅปบ็ซไธๅๅ็บ Square
็ๅญ้กๅฅใๆญค็ฏไพๆๅ่ช็ทไธ็คบไพใ
js
class Square extends Polygon {
constructor(length) {
// Here, it calls the parent class' constructor with lengths
// provided for the Polygon's width and height
super(length, length);
// Note: In derived classes, super() must be called before you
// can use 'this'. Leaving this out will cause a reference error.
this.name = "Square";
}
get area() {
return this.height * this.width;
}
}
ไฝฟ็จ extends
ๆผๅ
งๅปบ้กๅฅ
้ๅ็ฏไพๆดๅฑไบๅ
งๅปบ็ Date
้กๅฅใๆญค็ฏไพๆๅ่ช็ทไธ็ฏไพใ
js
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
๏ผไฝๆฐๅฐ่ฑก็ๅๅไธๆ็นผๆฟ Object.prototype
ใ
js
class nullExtends extends null {
constructor() {}
}
Object.getPrototypeOf(nullExtends); // Function.prototype
Object.getPrototypeOf(nullExtends.prototype); // null
new nullExtends(); //ReferenceError: this is not defined
ๆจๆบ
Specification |
---|
ECMAScriptยฎ 2026 Language Specification # sec-class-definitions |
็่ฆฝๅจ็ธๅฎนๆง
Loadingโฆ