goal
- 생성자
개발을 하다보면 비슷한 유형의 객체를 여러 개 만들어야 할 때가 있다.
: 사용자 정의 객체 타입 or 내장 객체 타입의 인스턴스를 생성
new constructor [ ([arguments]) ] |
---|
function Car (brand, model, color) {
this.brand = brand;
this.model = model;
this.color = color;
}
let myCar = new Car ("BMW", "M550i", "blue")
console.log(myCar); // Car {brand: "BMW", model: "M550i", color: "blue"}
console.log(myCar.brand); // BMW
console.log(myCar.model); // M550i
console.log(myCar.color); // blue
// usage : "반복(비슷한 유형의 객체 생성)"
------------------------------------------
let obj = new Object("delilah");
console.log(obj); // String {"delilah"}
// Object는 함수
return
문이 없음 : why?this
에 저장되고, this
는 자동으로 반환됨An instance is an object containing data and behavior described by the class. The new operator instantiates the class in JavaScript: instance = new Class()
아주 간단하게는 "객체"
참조형 객체는 객체에 대한 정보를 따로 저장하고 있으며, 메모리 주소 정보를 저장하기 때문에 다른 변수에서 같은 객체를 참조하는 것이 가능
참조형 타입으로 값을 할당하면 '객체'라고함
인스턴스와 객체는 참조형으로만 생성할 수 있음
다음편에서 다시 제대로 정리해보자. 계속~