[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Classes/constructor [Back]  [Original]

constructor - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

constructor

Baseline

20163

constructor

: constructor constructor Object.prototype.constructor

class Polygon {
  constructor() {
    this.name = "Polygon";
  }
}

const poly1 = new Polygon();

console.log(poly1.name);
// : "Polygon"

js
constructor() { /*  */ }
constructor(argument0) { /*  */ }
constructor(argument0, argument1) { /*  */ }
constructor(argument0, argument1, /* , */ argumentN) { /*  */ }

  • constructor
  • 1 constructor

js
class Person {
  constructor(name) {
    this.name = name;
  }

  introduce() {
    console.log(`${this.name}`);
  }
}

const otto = new Person("");

otto.introduce(); // 

js
constructor() {}

js
constructor(...args) {
  super(...args);
}

:

js
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

super()

js
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;
  }
}

new

  1. super() constructor this
  2. super()
  3. super() constructor

constructor this new new.target constructor this this this undefined TypeError

js
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

js
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

constructor

js
class Person {
  constructor(name = "") {
    this.name = name;
  }
  introduce() {
    console.log(`${this.name}`);
  }
}

const person = new Person();
person.introduce(); // 

js
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

constructor

classes sample ()

js
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() super() prototype super()

js
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