function Person(){}; //undefined let p0 = Person(); //undefined console.log(p0); //undefined
- 함수
Person을 선언하고 변수p0에Person함수를 담고 p0를 출력하게 되면 undefined가 출력된다.
그 이유는function Person(){};에 어떠한 값도 반환되지 않았기 때문에person()호출하게되면 어떠한 값도 출력되지 않는다.
let p = new Person(); console.log(p); //Person{}
- 하지만
Person앞에new를 붙이게 되면Person{}이라는 객체가 출력된다.
그이유는 함수 앞에new가 놓이게 되면 함수이름의 비어있는 객체를 만들고 그 값을p반환하게 되기 때문이다.- 이처럼 함수앞에
new가 붙으면생성자라고 부른다.