Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 1068554 of TypeError: "x" is not a constructor

  • Revision slug: Web/JavaScript/Reference/Errors/Not_a_constructor
  • Revision title: TypeError: "x" is not a constructor
  • Revision id: 1068554
  • Created:
  • Creator: fscholz
  • Is current revision? Yes
  • Comment Generators are not constructable either

Revision Content

{{jsSidebar("Errors")}}

Message

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

Error type

{{jsxref("TypeError")}}

What went wrong?

There was an attempt to use an object or a variable as a constructor, but that object or variable is not a constructor. See {{Glossary("constructor")}} or the new operator for more information on what a constructor is.

There are many global objects, like {{jsxref("String")}} or {{jsxref("Array")}}, which are constructable using new. However, some global objects are not and their properties and methods are static. The following JavaScript standard built-in objects are not a constructor: {{jsxref("Math")}}, {{jsxref("JSON")}}, {{jsxref("Symbol")}}, {{jsxref("Reflect")}}, {{jsxref("Intl")}}, {{jsxref("SIMD")}}, {{jsxref("Atomics")}}.

Generator functions cannot be used as constructors either.

Examples

Invalid cases

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

A car constructor

Suppose you want to create an object type for cars. You want this type of object to be called car, and you want it to have properties for make, model, and year. To do this, you would write the following function:

function Car(make, model, year) {
  this.make = make;
  this.model = model;
  this.year = year;
}

Now you can create an object called mycar as follows:

var mycar = new Car("Eagle", "Talon TSi", 1993);

See also

Revision Source

<div>{{jsSidebar("Errors")}}</div>

<h2 id="Message">Message</h2>

<pre class="syntaxbox">
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
</pre>

<h2 id="Error_type">Error type</h2>

<p>{{jsxref("TypeError")}}</p>

<h2 id="What_went_wrong">What went wrong?</h2>

<p>There was an attempt to use an object or a variable as a constructor, but that object or variable is not a constructor. See {{Glossary("constructor")}} or the <a href="/en-US/docs/Web/JavaScript/Reference/Operators/new"><code>new</code> operator</a> for more information on what a constructor is.</p>

<p>There are many global objects, like {{jsxref("String")}} or {{jsxref("Array")}}, which are constructable using <code>new</code>. However, some global objects are not and their properties and methods are static. The following JavaScript standard built-in objects are not a constructor: {{jsxref("Math")}}, {{jsxref("JSON")}}, {{jsxref("Symbol")}}, {{jsxref("Reflect")}}, {{jsxref("Intl")}}, {{jsxref("SIMD")}}, {{jsxref("Atomics")}}.</p>

<p><a href="/en-US/docs/Web/JavaScript/Reference/Statements/function*">Generator functions</a> cannot be used as constructors either.</p>

<h2 id="Examples">Examples</h2>

<h3 id="Invalid_cases">Invalid cases</h3>

<pre class="brush: js example-bad">
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
</pre>

<h3 id="A_car_constructor">A car constructor</h3>

<p>Suppose you want to create an object type for cars. You want this type of object to be called <code>car</code>, and you want it to have properties for make, model, and year. To do this, you would write the following function:</p>

<pre class="brush: js">
function Car(make, model, year) {
  this.make = make;
  this.model = model;
  this.year = year;
}
</pre>

<p>Now you can create an object called <code>mycar</code> as follows:</p>

<pre class="brush: js">
var mycar = new Car("Eagle", "Talon TSi", 1993);</pre>

<h2 id="See_also">See also</h2>

<ul>
 <li>{{Glossary("constructor")}}</li>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/new"><code>new</code> operator</a></li>
</ul>
Revert to this revision