What is the reason ES6 class constructors cant be called as normal functions?

2024/2/27 7:31:45

ES6 class constructors can't be called as normal functions. According to ES6 a TypeError should be raised when this is done. I used to think that classes were just syntactic sugar for a constructor function + functions in the prototype, but this makes it slightly not so.

I'm wondering, what was the rationale behind this? Unless I missed something, it prevents calling the function with a custom this, which could be desirable for some patterns.


A revisit to the ES6 spec shows how calling a Class function object without new is disabled by combining sections 9.2.9 and 9.2.1:

9.2.9 MakClassConstructor (F)
3. Set F’s [[FunctionKind]] internal slot to "classConstructor".

and when specifying the [[call]] method as opposed to the [[contruct]] method of a function:

(9.2.1) 2. If F’s [[FunctionKind]] internal slot is "classConstructor", throw a TypeError exception.

No restrictions are placed on calling function in section "11.2.3 "Function calls" of ES5.1.

So you are not missing anything: you can't use apply on a class constructor function.

The major rationale is probably both to make class extensions a fairly rigorous exercise, and to detect some early forms of error. For example you can't call Promise except as a constructor - and leaving out new before a call to Promise is a programming error. In regards extending classes, note that the constructor property of class instances is correctly set (the last class after possibly multiple extensions) and the the .prototype property of the class constructor is read only - you can't dynamically change the prototype object used to construct class instances, even though you could change the prototype property of a constructor function.

I used to think classes were syntactic sugar but have moved away from the concept.


Related Q&A

How to do skip and take functions in javascript for Json array

I want to do that in javascript:for (int i = 0; i <= pieces; i++){List<product> piecesProuducts = productList.Skip(i * 2).Take(2).ToList();}I have a json array. I want to get two records block…

Prevent certain HTML elements from being copied

Im not entirely sure how to go about researching this idea. Im sure its been done, but Im having an issue articulating it for an effective Google search.I have a results page that has the option to dow…

Why {} || {} throws syntax error?

This works:var a = {} || {};While this does not:{} || {}; Uncaught SyntaxError: Unexpected token ||at Object.InjectedScript._evaluateOn (<anonymous>:905:140)at Object.InjectedScript._evaluateAndW…

pass input file to background script

I want to pass the input file from content page to extension background script, and then load it with FileReader() in the extension background script.So in the web page I have a <input type="fi…

How to match nested function invocations (bracket pairs) using a regular expression (recursive?)

Im looking for a regular expression (**) that will match an unknown number of nested functions. Soexpression function(expression) function(function(expression)) function(function(function(expression)))…

JAVA servlets - open message popup

I want to user HttpServletResponse object to compose a response that will tell the browser client to open a popup with some message - how can i do that?

How do I draw images in layers on canvas?

I have a canvas where I use drawImage to draw a bunch of images to the canvas. How I want the result to be: I want the first image i draw to be on layer 1, the next image on layer 2 and so on What real…

How can I perform in-browser contrast stretching/normalization?

I have jpegs on a webpage. I would like to perform client-side equalization (contrast stretching) on these images without browser plugins. I would also accept a solution for histogram equalization.I cu…

Accessing ExtendedData information via Google Maps API v3

I have a KML file which contains within each <Placemark> node an <ExtendedData> node, and then a number of <Data> nodes with key/value pairs. Ive followed the examples at: http://code…

Does JSON.stringify() always ignore methods when parsing objects?

I found the following in the Mozilla-documentation:If undefined, a function, or an XML value is encountered during conversion it is either omitted (when it is found in an object) or censored to null (w…