The question that arose after reading this article on MDN .

As you know, any function can be used as a constructor, that is, calling it with new will create objects.

 const One = function(name) { this.name = name; }; const vasya = new One('vasya'); 

All is well. The function created an empty object, through this received a link to it, added the name property.

Consider another example:

 const Two = (name) => { this.name = name; }; const petya = new Two('petya'); 

Nothing special, except:

Uncaught TypeError: Two is not a constructor (...)


Question:
Why in the second example the pointer function cannot be a constructor?
What is the specificity of her work?

Why doesn't the switch function have [[Prototype]] ?
What if...

 Two.prototype = Object.create({ hasName: () => console.log('Yes, it has a name') }); Two.prototype.constructor = Two; 

It will not work, since [[Prototype]] is an internal property, but in the name of what is this done for switch functions?

    2 answers 2

    Inheritance in JS is largely dependent on this .
    Arrow functions are largely trimmed relative to older brothers, including they do not have their own this , which is very convenient for callback and they are fast.


    But you have to pay for it, optimization is carried out due to the following features:

    1. Arrow function cannot be used as a constructor.
    2. The variables this , arguments and super are taken from the parent function scope .
    3. Inside the arrow function you cannot use your own arguments object — which means that you do not need to synchronize the values ​​of the arguments and the arguments pseudo-array.
    4. The call , apply and bind functions cannot change the context.

      The switch functions do not have their own context, and this in them is this from the context a higher level.

      In the case of OOP (or rather, an attempt to use OOP in js), the arrow functions can be perceived as class methods (which is created by an ordinary function). Read more here: https://developer.mozilla.org/ru/docs/Web/JavaScript/Reference/Functions/Arrow_functions