★ wanayoo — archive 1999 https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Classes/constructorNouvelle recherche | Portail wanayoo

constructor

공헌자 숫자: 3명
아직 자원 봉사자들이 한국어로 현재 문서를 번역하지 않았습니다. 가입해서 이 문서가 번역되는 일에 함께 해 주세요!

This is a new technology, part of the ECMAScript 2015 (ES6) standard .
This technology's specification has been finalized, but check the compatibility table for usage and implementation status in various browsers.

The constructor method is a special method for creating and initializing an object created with a class.

Syntax

constructor([arguments]) { ... }

Description

There can only be one special method with the name "constructor" in a class. A SyntaxError will be thrown, if the class contains more than one occurrence of a constructor method.

A constructor can use the super keyword to call the constructor of a parent class.

Examples

This code snippet is taken from the classes sample (live demo).

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

  set area(value) {
    this.area = value;
  } 
}

Specifications

Specification Status Comment
ECMAScript 2015 (6th Edition, ECMA-262)
The definition of 'Constructor Method' in that specification.
Standard Initial definition.

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support 42.0 Nightly build ? ? ?
Feature Android Android Webview Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support Not supported 42.0 Nightly build ? ? ? 42.0

Firefox-specific notes

  • Default constructors are not implemented yet (bug 1105463)

See also

문서 태그 및 공헌자

Contributors to this page: jpmedley, fscholz, llue
최종 변경: jpmedley,
사이드바 숨기기