생성자 함수(constructor) : new 연산자와 함께 호출하여 객체(인스턴스)를 생성하는 함수
인스턴스 (instance) : 생성자 함수에 의해 생성된 객체
new 연산자와 함께 Object 생성자 함수를 호출하면 빈 객체를 생성하여 반환한다.
// 빈 객체의 생성
const person = new Object();
// 생성자 함수
function Circle(radius){
this.radius = radius;
this.getDiameter = function() {
return 2 * this.radius;
};
}
const Circle1 = new Circle(5); // 반지름이 5인 Circle 객체를 생성
const Circle2 = new Circle(10); // 반지름이 10인 Circle 객체를 생성
: 객체 자신의 프로퍼티나 메서드를 참조하기 위한 자기 참조 변수(self-referencing variable)
// 생성자 함수
function Circle(radius){
// 인스턴스 초기화
this.radius = radius;
this.getDiameter = function(){
return 2 * this.radius;
};
}
// 인스턴스 생성
const circle1= new Circle(5);
바인딩(binding)
: 식별자와 값을 연결하는 과정
ex. 변수 선언은 변수의 이름(식별자)과 확보된 메모리 공간의 주소를 바인딩하는 것이다.
this 바인딩 : this(키워드, 식별자)와 this가 가리킬 객체를 바인딩 하는 것
일반 객체는 호출할 수 없지만 함수는 호출할 수 있다.
callable: 내부 메서드 [[Call]]을 갖는 함수 객체, 호출할 수 있는 객체인 함수를 뜻함
constructor: 내부 메서드 [[Construct]]를 갖는 함수 객체, 생성자 함수로서 호출할 수 있는 함수를 뜻함
non-constructor: [[Construct]]를 갖지 않는 함수 객체, 객체를 생성자 함수로서 호출할 수 없는 함수를 뜻함
constructor: 함수 선언문, 함수 표현식, 클래스
non-constructor: 메서드, 화살표 함수
new 연산자와 함께 호출하는 함수는 constructor 이다.