| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Classes/constructor | [Back] [Original] |
Get to know MDN better
class Polygon {
constructor() {
this.name = "Polygon";
}
}
const poly1 = new Polygon();
console.log(poly1.name);
// : "Polygon"
constructor() { /* */ }
constructor(argument0) { /* */ }
constructor(argument0, argument1) { /* */ }
constructor(argument0, argument1, /* , */ argumentN) { /* */ }
class Person {
constructor(name) {
this.name = name;
}
introduce() {
console.log(`${this.name}`);
}
}
const otto = new Person("");
otto.introduce(); //
constructor() {}
constructor(...args) {
super(...args);
}
class ValidationError extends Error {
printCustomerMessage() {
return `Validation failed :-( (details: ${this.message})`;
}
}
try {
throw new ValidationError("Not a valid phone number");
} catch (error) {
if (error instanceof ValidationError) {
console.log(error.name); // ValidationError
console.log(error.printCustomerMessage());
} else {
console.log("Unknown error", error);
throw error;
}
}
ValidationError
Error
class ValidationError extends Error {
constructor(message) {
super(message); //
this.name = "ValidationError";
this.code = "42";
}
printCustomerMessage() {
return ` :-( (details: ${this.message}, code: ${this.code})`;
}
}
try {
throw new ValidationError("");
} catch (error) {
if (error instanceof ValidationError) {
console.log(error.name); // ValidationError
console.log(error.printCustomerMessage());
} else {
console.log("", error);
throw error;
}
}
constructor this new new.target constructor this this this undefined TypeError
new (class C extends class B {
constructor() {
console.log(this.foo());
}
} {
#a = 1;
foo() {
return this.#a; // TypeError: Cannot read private member #a from an object whose class did not declare it
//
//
//
}
})();
constructor undefined TypeError
class ParentClass {
constructor() {
return 1;
}
}
console.log(new ParentClass()); // ParentClass {}
//
class ChildClass extends ParentClass {
constructor() {
return 1;
}
}
console.log(new ChildClass()); // TypeError: Derived constructors may only return object or undefined
class Person {
constructor(name = "") {
this.name = name;
}
introduce() {
console.log(`${this.name}`);
}
}
const person = new Person();
person.introduce(); //
class Foo {
//
["constructor"]() {
console.log("called");
this.a = 1;
}
}
const foo = new Foo(); //
console.log(foo); // Foo {}
foo.constructor(); // "called"
console.log(foo); // Foo { a: 1 }
constructor #constructor constructor
class Square extends Polygon {
constructor(length) {
//
//
super(length, length);
// : `this` `super()`
// ReferenceError
this.name = "Square";
}
get area() {
return this.height * this.width;
}
set area(value) {
this.height = value ** 0.5;
this.width = value ** 0.5;
}
}
super() super() prototype super()
class Polygon {
constructor() {
this.name = "Polygon";
}
}
class Rectangle {
constructor() {
this.name = "Rectangle";
}
}
class Square extends Polygon {
constructor() {
super();
}
}
// Polygon Rectangle
Object.setPrototypeOf(Square, Rectangle);
const newInstance = new Square();
// newInstance Polygon
// Square.prototype
// newInstance
// newInstance --> Square.prototype --> Polygon.prototype
console.log(newInstance instanceof Polygon); // true
console.log(newInstance instanceof Rectangle); // false
// super() Rectangle
// newInstance name Rectangle
console.log(newInstance.name); // Rectangle
| ECMAScript 2027 LanguageSpecification # sec-static-semantics-constructormethod |
| Web Proxy Viewer | New URL | Original Page |