Skip to content
Advertisement

How to check if a Javascript function is a constructor

I noticed not all the Javascript functions are constructors.

JavaScript

Question 1: How do I check if a function is a constructor so that it can be called with new keyword?

Question 2: When I create a function, is it possible to make it NOT a constructor?

Advertisement

Answer

A little bit of background:

ECMAScript 6+ distinguishes between callable (can be called without new) and constructable (can be called with new) functions:

  • Functions created via the arrow functions syntax or via a method definition in classes or object literals are not constructable.
  • Functions created via the class syntax are not callable.
  • Functions created in any other way (function expression/declaration, Function constructor) are callable and constructable.
  • Built-in functions are not constructrable unless explicitly stated otherwise.

About Function.prototype

Function.prototype is a so called built-in function that is not constructable. From the spec:

Built-in function objects that are not identified as constructors do not implement the [[Construct]] internal method unless otherwise specified in the description of a particular function.

The value of Function.prototype is create at the very beginning of the runtime initialization. It is basically an empty function and it is not explicitly stated that it is constructable.


How do I check if an function is a constructor so that it can be called with a new?

There isn’t a built-in way to do that. You can try to call the function with new, and either inspect the error or return true:

JavaScript

However, that approach is not failsafe since functions can have side effects, so after calling f, you don’t know which state the environment is in.

Also, this will only tell you whether a function can be called as a constructor, not if it is intended to be called as constructor. For that you have to look at the documentation or the implementation of the function.

Note: There should never be a reason to use a test like this one in a production environment. Whether or not a function is supposed to be called with new should be discernable from its documentation.

When I create a function, how do I make it NOT a constructor?

To create a function is truly not constructable, you can use an arrow function:

JavaScript

Arrow functions are by definition not constructable. Alternatively you could define a function as a method of an object or a class.

Otherwise you could check whether a function is called with new (or something similar) by checking it’s this value and throw an error if it is:

JavaScript

Of course, since there are other ways to set the value of this, there can be false positives.


Examples

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement