JavaScriptFundamental (ES6), part 1
Why you cannot use new with an arrow function in JavaScript
10 An arrow cannot be newed
fairTry to use new on an arrow, on an old-style constructor function, on a class called without new, and on a method shorthand. Report which combinations the language allows.
Stuck? Three hints, smallest first
- 1. An arrow has no
prototypeproperty, so there is nothing fornewto attach an instance to. - 2. A
classis the mirror image: it insists onnewand throws without it. - 3. Method shorthand (
{ make() {} }) is not a constructor either, though{ make: function () {} }is.
const Point = (x, y) => ({ x, y });
const newedArrow = (() => {
try { return new Point(1, 2); } catch (e) { return e.constructor.name; }
})();
function Legacy(x) { this.x = x; }
class Money { constructor(amount) { this.amount = amount; } }
const classWithoutNew = (() => {
try { return Money(500); } catch (e) { return e.constructor.name; }
})();
const tools = { make() { return 'made'; }, older: function () { return 'made'; } };
const newedMethod = (() => {
try { return new tools.make(); } catch (e) { return e.constructor.name; }
})();
const newedOlder = (() => {
try { new tools.older(); return 'allowed'; } catch (e) { return e.constructor.name; }
})();
console.log('new on an arrow :', newedArrow);
console.log('arrow has a prototype? :', 'prototype' in Point);
console.log('a function declaration? :', typeof Legacy.prototype);
console.log('calling a class plainly :', classWithoutNew);
console.log('new on method shorthand :', newedMethod, ' on a function property:', newedOlder);
Output
9 checks that prove this answer
You do not have to read these. They are part of the answer panel above, so pressing
Run there executes all 9 of them and prints ok for each.
That output is the proof, and the build fails if a single check fails.
test('new on an arrow throws', newedArrow, 'TypeError');
test('because an arrow has no prototype property', 'prototype' in Point, false);
test('a function declaration does have one', typeof Legacy.prototype, 'object');
test('called plainly, the arrow is a perfectly good factory', Point(1, 2), { x: 1, y: 2 });
test('the old constructor pattern still works', new Legacy(3).x, 3);
test('a class refuses to be called without new', classWithoutNew, 'TypeError');
test('method shorthand is not a constructor', newedMethod, 'TypeError');
test('but an old-style function property still is', newedOlder, 'allowed');
test('an arrow does keep length and an inferred name', [Point.length, Point.name], [2, 'Point']);
Three different things go under the name "function", and new is the test that separates them. An arrow is a value you call. A function declaration is callable and constructible, which is why the pre-class world used it for both. A class is constructible only.
The last pair is the one that catches people mid-refactor: rewriting { make: function () {} } as { make() {} } looks like pure tidying, and it silently removes the ability to new it. Same for =>. If something is meant to be constructed, say class.