비슷한 성질을 가진 여러개의 객체를 만들기 위해, 일종의 설계도라고 할 수 있는 생성자 함수(Constructor)를 만들어 찍어내듯 사용하는데 이렇게 생성된 객체를 인스턴스라 부를 수 있다.
좀 더 알기 쉽게 칼을 만드는 과정으로 비유하여 다음과 같은 예시 코드를 작성해봤다.
생성자 함수(Constructor) = 거푸집
인스턴스 = 거푸집으로 찍어낸 칼
function Sword(color, metal) {
this.color = color;
this.metal = metal;
this.is = function() {
console.log(`This is ${this.color} ${this.metal} sword!`);
};
}
const redSteel = new Sword('red', 'steel');
console.log(redSteel); //Sword {color: 'red', metal: 'steel', is: ƒ}
redSteel.is(); //This is red steel sword!
객체지향언어에서 흔히 사용되는 클래스(Class)가 자바스크립트에서는 프로토타입(prototype)이며 생성자 함수가 사용된다. 다시 말해 클래스나 프로토타입을 사용하여 만들어 낸 것이 결과물이 인스턴스라고 할 수 있다.
이렇게 생성된 인스턴스는 원래의 객체인 클래스나 프로토타입이 가지고 있는 프로퍼티(property)와 메소드(method)를 모두 상속(inheritance)받는다!