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?