This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

생성자 (Constructor)

생성자는 인스턴스화된 특정 클래스 객체에 속합니다. 생성자는 이 객체를 초기화하고 해당 개인 정보에 대한 접근을 제공할 수 있습니다. 생성자의 개념은 대부분의 객체 지향 프로그래밍 (object-oriented programming, OOP) 언어에 적용될 수 있습니다. 기본적으로, JavaScript의 생성자는 일반적으로 class의 인스턴스에서 선언됩니다.

문법

js
// 일반적인 기본 생성자 클래스입니다.
function Default() {}

// 매개변수를 통해 오버로드된 생성자 클래스입니다.
function Overloaded(arg1, arg2, /* …, */ argN) {}

JavaScript에서 클래스의 생성자를 호출하려면, new 연산자를 사용하여 새 객체 참조변수에 할당합니다.

js
function Default() {}

// 지역 변수 defaultReference에 할당된 기본 객체의 새로운 참조
const defaultReference = new Default();

같이 보기