💊  자바스크립트는 Object 생성자 함수외에도 String, Number, Boolean, Function, Array, Date, RegExp, Promise등의 빌트인 생성자함수를 제공한다

// Object 생성자 함수에 의한 Object 객체 생성
const myObj = new Object()
console.log("myObj type : ", myObj);
console.log("-->",myObj)

// String 생성자 함수에 의한 String 객체 생성
const strObj = new String('Lee');
console.log("strObj type : ",typeof strObj); // object
console.log("-->",strObj);

// Number 생성자 함수에 의한 Number 객체 생성
const numObj = new Number(123);
console.log("numObj type : ",typeof numObj);
console.log("-->", numObj);

// Boolean 생성자 함수에 의해 Boolean 객체 생성
const boolObj = new Boolean(true);
console.log("boolObj type : ",typeof boolObj);
console.log("-->",boolObj);

// Function 생성자 함수에 의해 Function 객체 생성
const func = new Function('x', 'return x *x');
console.log("func type : ",typeof func);
console.dir(func) // 🎯 참조

// Array 생성자 함수에 의해 Array 객체 생성
const arr = new Array(1,2,3);
console.log("arr type : ",typeof arr);
console.log("-->",arr);

// Date 생성자 함수에 의해 Date객체 생성
const date = new Date();
console.log("date type : ",typeof date)
console.log("-->",date)

// RegExp 생성자 함수에 의해 RegExp 객체 생성
const regExp = new RegExp(/ab+c/i);
console.log("regExp type : ",typeof regExp)
console.log("-->",regExp)

🎯 console.dir() 주어진 자바스크립틔 객체 속성을 인터랙티브한 목록으로 표시한다. 출력된 결과는 자식 객체의 내용을 볼 수 있는 ▶︎ 함께 계층적 목록으로 나타난다.

💊  객체를 생성하는 방법에는 객체 리터럴을 사용하는 것도 있다.

const circle1 = {
  radius :5,
  getArea (){
    return 2*this.radius
  }
}
console.log(circle1.getArea())

객체리터럴을 사용해서 객체를 만들수 있지만 이 방식은 단 하나의 객체만 생성한다. 따라서 같은 프로퍼티를 갖는 객체를 여러개 생성하려면 비슷한 코드를 아래처럼 작성해야 한다.

console.log(circle1.getArea())
const circle2 = {
  radius :3,
  getArea (){
    return 2*this.radius
  }
}
console.log(circle2.getArea())

이 경우에 radius의 값은 바뀔지라도 getArea() 은 동일하다. 그러므로 프로퍼티와 메소드를 수십개 생성하는것은 문제가 된다.

그래서 생성자 함수에 의한 객체 생성방식은 객체(인스턴스)를 생성하기 위한 템플릿(클래스)를 만드는 것과 같다.

function Circle(radius){
  this.radius = radius;
  this.getArea = function() {
    return 2*this.radius;
  }
}
const circle1 = new Circle(5);
const circle2= new Circle(7);
console.log(circle1.getArea())
console.log(circle2.getArea())

1. new 와 생성자를 이용한 객체

💊  this