TypeError: "x" is not a constructor

๋ฉ”์„ธ์ง€

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

์˜ค๋ฅ˜ ์œ ํ˜•

TypeError

๋ญ๊ฐ€ ์ž˜๋ชป๋œ ๊ฑฐ์ฃ ?

๊ฐ์ฒด ํ˜น์€ ๋ณ€์ˆ˜๋ฅผ ์ƒ์„ฑ์ž๋กœ ์‚ฌ์šฉํ•˜๋ ค๊ณ  ํ–ˆ์Šต๋‹ˆ๋‹ค, ํ•˜์ง€๋งŒ ๊ฐ์ฒด(ํ˜น์€ ๋ณ€์ˆ˜)๊ฐ€ ์ƒ์„ฑ์ž๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค. ์ƒ์„ฑ์ž๊ฐ€ ๋ฌด์—‡์ธ์ง€์— ๋Œ€ํ•œ ์ž์„ธํ•œ ์ •๋ณด๋Š” constructor (en-US) ํ˜น์€ 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);

๊ฐ™์ด ๋ณด๊ธฐ