new 연산자와 함께 Object 생성자 함수를 호출하면 빈 객체를 생성하여 반환함
이후 프로퍼티(메소드 포함)들을 추가하여 객체를 완성할 수 있음
const student = Object();
const student = {};
console.log(Object); // Object()도 함수임을 확인
// [Function: Object]
student.name = '유관순';
student.age = 16;
const student1 = {
name : '유관순',
age : 16,
getInfo : function() {
return `${this.name}(은)는 ${this.age}세입니다.`;
}
};
const student2 = {
name : '홍길동',
age : 20,
getInfo : function() {
return `${this.name}(은)는 ${this.age}세입니다.`;
}
};
객체 리터럴 방식의 객체 생성 방식은 직관적이고 간편하지만, 단 하나의 객체만을 생성함
객체를 생성하기 위한 프로퍼티들을 하나의 템플릿 개념으로 생성자 함수로써 작성하면 동일한 프로퍼티를 가지는 여러 객체를 쉽게 생성할 수 있음
function Student(name, age) {
this.name = name;
this.age = age;
this.getInfo = function() {
return `${this.name}(은)는 ${this.age}세입니다.`;
}
}
/* 생성자 함수를 활용한 인스턴스 생성 */
const student3 = new Student('장보고', 30);
const student4 = new Student('신사임당', 40);