Oops, All Code!/📝 Study Notes
[JS] 빌트인 함수와 new 연산자
밍동망동
2023. 3. 12. 00:00
대부분의 빌트인 함수는 new 연산자와 상관 없이 반응하는 경우도 있다.
Object와 Function 생성자 함수의 경우가 그러하다.
이 둘은 new 연산자 없이도 동일하게 동작한다.
let obj = new Object();
console.log(obj);
obj = Object();
console.log(obj);
let func = new Function('a', 'return a + a');
console.log(func);
func = Function('a', 'return a + a');
console.log(func);
반면 String, Number, Boolean 생성자 함수는 new 연산자와 함께 호출하면 객체를 반환하지만
new 연산자 없이 호출하면 각각 문자열, 숫자, 불리언 값을 반환한다.
이는 데이터 타입 변환에 자주 사용된다.
const str = String(03245); // string
const num = Number('03245'); // number
const bool = Boolean('true'); // boolean