๋ฉ์ธ์ง
TypeError: "x" is not a constructor TypeError: Math is not a constructor TypeError: JSON is not a constructor TypeError: Symbol is not a constructor TypeError: Reflect is not a constructor TypeError: Intl is not a constructor TypeError: SIMD is not a constructor TypeError: Atomics is not a constructor
์ค๋ฅ ์ ํ
๋ญ๊ฐ ์๋ชป๋ ๊ฑฐ์ฃ ?
๊ฐ์ฒด ํน์ ๋ณ์๋ฅผ ์์ฑ์๋ก ์ฌ์ฉํ๋ ค๊ณ ํ์ต๋๋ค, ํ์ง๋ง ๊ฐ์ฒด(ํน์ ๋ณ์)๊ฐ ์์ฑ์๊ฐ ์๋๋๋ค. ์์ฑ์๊ฐ ๋ฌด์์ธ์ง์ ๋ํ ์์ธํ ์ ๋ณด๋ constructor ํน์ new
operator ๋ฅผ ์ฐธ์กฐํ์๊ธฐ ๋ฐ๋๋๋ค.
String
ํน์ Array
์ ๊ฐ์ด new
,๋ฅผ ์ฌ์ฉํ์ฌ ์์ฑํ ์ ์๋ ์ ์ญ ๊ฐ์ฒด๋ค์ด ์์ต๋๋ค. ํ์ง๋ง ์ผ๋ถ ์ ์ญ ๊ฐ์ฒด๋ค์ ๊ทธ๋ ์ง ์๊ณ ์์ฑ๊ณผ ๋ฉ์๋๊ฐ ์ ์ ์
๋๋ค. ๋ค์์ ์๋ฐ์คํฌ๋ฆฝํธ ํ์ค ๋ด์ฅ ๊ฐ์ฒด๋ค์ ์์ฑ์๊ฐ ์๋๋๋ค: Math
, JSON
, Symbol
, Reflect
, Intl
, SIMD
, Atomics
.
Generator functions ๋ํ ์์ฑ์๋ก ์ฌ์ฉ๋ ์ ์์ต๋๋ค.
์์
์ ํจํ์ง ์์ ๊ฒฝ์ฐ
var Car = 1;
new Car();
// TypeError: Car is not a constructor
new Math();
// TypeError: Math is not a constructor
new Symbol();
// TypeError: Symbol is not a constructor
function* f() {};
var obj = new f;
// TypeError: f is not a constructor
car ์์ฑ์
์๋์ฐจ(car) ๊ฐ์ฒด๋ฅผ ๋ง๋ค๊ณ ์ ํ๋ค๊ณ ๊ฐ์ ํฉ๋๋ค. ์ด ๊ฐ์ฒด์ ํ์
์ car
๋ผ ํ๊ณ make, model, and year ์ธ ๊ฐ์ ํ๋กํผํฐ๋ฅผ ๊ฐ์ต๋๋ค. ์ด๋ฅผ ์ํด ๋ค์๊ณผ ๊ฐ์ ํจ์๋ฅผ ์์ฑํ ๊ฒ์
๋๋ค:
function Car(make, model, year) {
this.make = make;
this.model = model;
this.year = year;
}
์ด์ ๋ค์๊ณผ ๊ฐ์ด mycar
๋ผ ๋ถ๋ฆฌ๋ ๊ฐ์ฒด๋ฅผ ์์ฑํ ์ ์์ต๋๋ค:
var mycar = new Car('Eagle', 'Talon TSi', 1993);
ํ๋ผ๋ฏธ์ค ์ด์ฉ
์ฆ์ ์คํ๋๋ ํ๋ผ๋ฏธ์ค๋ฅผ ๋ฐํํ๋ ๊ฒฝ์ฐ์๋ ์๋ก์ด Promise(...)๋ฅผ ์์ฑํ ํ์๊ฐ ์์ต๋๋ค.
์๋๋ ์ฌ๋ฐ๋ฅธ ๋ฐฉ๋ฒ์ด ์๋๋๋ค(ํ๋ผ๋ฏธ์ค ์์ฑ์๊ฐ ์ ๋๋ก ํธ์ถ๋๊ณ ์์ง ์์ต๋๋ค). TypeError: this is not a constructor
์์ธ๋ฅผ ๋์ง๊ฒ ๋ฉ๋๋ค:
return new Promise.resolve(true);
๋์ , Promise.resolve() ํน์ Promise.reject() ์ ์ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ญ์์ค:
// This is legal, but unnecessarily long:
return new Promise((resolve, reject) => { resolve(true); })
// Instead, return the static method:
return Promise.resolve(true);
return Promise.reject(false);