| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Classes/extends#extending_null | [Back] [Original] |
Get to know MDN better
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());
// : "19-Aug-1975"
class ChildClass extends ParentClass { /* */ }
ParentClass null
extends
function OldStyleClass() {
this.someProperty = 1;
}
OldStyleClass.prototype.someMethod = function () {};
class ChildClass extends OldStyleClass {}
class ModernClass {
someProperty = 1;
someMethod() {}
}
class AnotherChildClass extends ModernClass {}
ParentClass prototype Object null prototype new
function ParentClass() {}
ParentClass.prototype = 3;
class ChildClass extends ParentClass {}
// Uncaught TypeError: Class extends value does not have valid prototype property 3
console.log(Object.getPrototypeOf(new ParentClass()));
// [Object: null prototype] {}
// Not actually a number!
extends ChildClass ChildClass.prototype
ChildClass |
ChildClass.prototype |
|
|---|---|---|
extends |
Function.prototype |
Object.prototype |
extends null |
Function.prototype |
null |
extends ParentClass |
ParentClass |
ParentClass.prototype |
class ParentClass {}
class ChildClass extends ParentClass {}
//
Object.getPrototypeOf(ChildClass) === ParentClass;
//
Object.getPrototypeOf(ChildClass.prototype) === ParentClass.prototype;
extends extends this this ReferenceError await yield
class SomeClass extends class {
constructor() {
console.log("");
}
} {
constructor() {
super();
console.log("");
}
}
new SomeClass();
//
//
undefined
class ParentClass {
constructor() {
return 1;
}
}
console.log(new ParentClass()); // ParentClass {}
//
//
class ChildClass extends ParentClass {
constructor() {
super();
return 1;
}
}
console.log(new ChildClass()); // TypeError: Derived constructors may only return object or undefined
Promise.resolve() Array.from()Promise.prototype.then() Array.prototype.map() Promise then() catch() Map set() Map() this [p1, p2, p3].map(Promise.resolve) Promise.resolve this Array.from() this this.constructor new this.constructor() [Symbol.species] this [Symbol.species] Map() x set() x extends null Object.prototype super() TC39
new (class extends null {})();
// TypeError: Super constructor null of anonymous class is not a constructor
new (class extends null {
constructor() {}
})();
// ReferenceError: Must call super constructor in derived class before accessing 'this' or returning from derived constructor
new (class extends null {
constructor() {
super();
}
})();
// TypeError: Super constructor null of anonymous class is not a constructor
class NullClass extends null {
constructor() {
// new.target
//
return Object.create(new.target.prototype);
}
}
const proto = Object.getPrototypeOf;
console.log(proto(proto(new NullClass()))); // null
class Square extends Polygon {
constructor(length) {
//
// Polygon
super(length, length);
// : 'this' super()
//
this.name = "Square";
}
get area() {
return this.height * this.width;
}
}
const Animal = {
speak() {
console.log(`${this.name}`);
},
};
class Dog {
constructor(name) {
this.name = name;
}
}
Object.setPrototypeOf(Dog.prototype, Animal);
const d = new Dog("");
d.speak(); //
class MyDate 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()}`;
}
}
Object JavaScript Object.prototype extends Object extends Object.keys() Object Object this
Object() super() new.target.prototype super()
class C extends Object {
constructor(v) {
super(v);
}
}
console.log(new C(1) instanceof Number); // false
console.log(C.keys({ a: 1, b: 2 })); // [ 'a', 'b' ]
function MyObject(v) {
return new Object(v);
}
class D extends MyObject {
constructor(v) {
super(v);
}
}
console.log(new D(1) instanceof Number); // true
MyArray Array species
Array.prototype.map() MyArray Array Symbol.species
class MyArray extends Array {
// Array species
static get [Symbol.species]() {
return Array;
}
}
const a = new MyArray(1, 2, 3);
const mapped = a.map((x) => x * x);
console.log(mapped instanceof MyArray); // false
console.log(mapped instanceof Array); // true
1
const calculatorMixin = (Base) =>
class extends Base {
calc() {}
};
const randomizerMixin = (Base) =>
class extends Base {
randomize() {}
};
class Foo {}
class Bar extends calculatorMixin(randomizerMixin(Foo)) {}
ReadOnlyMap
class ReadOnlyMap extends Map {
set() {
throw new TypeError("A read-only map must be set at construction time.");
}
}
Map() set()
const m = new ReadOnlyMap([["a", 1]]); // TypeError: A read-only map must be set at construction time.
class ReadOnlyMap {
#data;
constructor(values) {
this.#data = new Map(values);
}
get(key) {
return this.#data.get(key);
}
has(key) {
return this.#data.has(key);
}
get size() {
return this.#data.size;
}
*keys() {
yield* this.#data.keys();
}
*values() {
yield* this.#data.values();
}
*entries() {
yield* this.#data.entries();
}
*[Symbol.iterator]() {
yield* this.#data[Symbol.iterator]();
}
}
ReadOnlyMap Map ReadOnlyMap Map Map Map set() getOrInsert() ReadOnlyMap getOrInsert() ReadOnlyMap set
| ECMAScript 2027 LanguageSpecification # sec-class-definitions |
| Web Proxy Viewer | New URL | Original Page |