Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. JavaScript
  3. Reference
  4. JavaScript error reference
  5. TypeError: class constructors must be invoked with 'new'

TypeError: class constructors must be invoked with 'new'

The JavaScript exception "class constructors must be invoked with 'new'" occurs when aclass constructor is called without thenew keyword. All class constructors must be called withnew.

Message

TypeError: Class constructor X cannot be invoked without 'new' (V8-based)TypeError: Class constructors cannot be invoked without 'new' (V8-based)TypeError: class constructors must be invoked with 'new' (Firefox)TypeError: Cannot call a class constructor without |new| (Safari)

Error type

TypeError

What went wrong?

In JavaScript,calling a function withoutnew andconstructing a function withnew are two distinct operations, and functions can behave differently depending on how they are called.

Traditionally, JavaScript functions have been used as both constructors and normal functions, and can detect how they were called usingnew.target. However, class constructors are always constructors and cannot be called as normal functions.

Examples

Invalid cases

js
class X {}X(); // TypeError: class constructors must be invoked with 'new'

Valid cases

js
class X {}new X();

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp