extending built-in classes
โข๏ธ Alertโ
Normally, when one class extends another, both static and non-static methods are inherited. ( ๐ JS.info โฉ Static properties and methods )
But built-in classes are an exception. They donโt inherit statics from each other.โ๏ธโ๏ธโ๏ธ
ๆฏ่ผ class B extends A ่ Date extends Object ็ไธๅ๏ผ
ไธ่ฌ็ class A, B๏ผๅฎๅไน้ๆ prototype ็้ฃ็ต๏ผๆไปฅ A ๆๆ็ static properties B ไน้ฝๆใ
ไฝ Date ่ Object ไน้ไธฆๆฒๆ้ฃ็ต๏ผๅ ๆญค้็ถๆ
Object.keys()ๆนๆณ๏ผไฝๅปๆฒๆDate.keys()ๆนๆณโ๏ธ

// โญ extending Array
class PowerArray extends Array {
// arr.isEmpty
get isEmpty() { return this.length === 0 }
// arr.last
get last() { return this[this.length - 1] }
}
let arr = new PowerArray(1, 2, 5, 10, 50);
let filtered = arr.filter(x => x >= 10); // โญ return `PowerArray`โ
arr.isEmpty, // false
arr.last, // 50
Array.isArray(arr), // true
filtered, // PowerArray(2) [ 10, 50 ] โญ
filtered.isEmpty, // false
PowerArray.__proto__, // Array
PowerArray.isArray(filtered), // true (โญ static methods inheritedโ)Last updated
Was this helpful?