Limitations: No Prototype and new.target Property
Learn about the limitations of arrow functions, specifically how they lack the prototype property and the new.target property.
new.target
is lexically scoped
Functions may use new.target
to determine if they were called as a constructor or as a regular function.
For non-arrow functions
In the following code, we use the new.target
property to check how the function was called.
Press + to interact
'use strict';//START:CODEconst f1 = function() {if(new.target) {console.log('called as a constructor');}else {console.log('called as a function');}};new f1();f1();//END:CODE
If the function is called as a constructor, new.target
refers to the constructor function; otherwise, it’s undefined. The previous code’s output confirms this behavior.
For arrow functions
Generally speaking, arrow functions don’t have the new.target
property. This makes sense because arrow functions can’t be invoked as constructors. However, if we reference this property within an arrow function we will not get a “new.target is not
...