HappyCoding HappyCoding - 3 months ago 5
Javascript Question

How do a normal function and arrow function differ around _proto_ constructors?



let anonymous = function () {
return 'hello'
};

let f = () => 'world';

console.log(new anonymous['__proto__'].constructor());
console.log(new f['__proto__'].constructor());


console.log(anonymous());





Both functions return a function with the name is
anonymous
when I try to create new instance of it.

My question: when I call
anonymous()
function, why doesn't it hit to the second constructor (arrow function)?

Answer

The [[Prototype]] of both functions is Function.prototype. In that sense, function expressions and arrow functions do not differ.

let anonymous = function () {
  return 'hello'
};
let f = () => 'world';
console.log(Object.getPrototypeOf(anonymous) === Function.prototype); // true
console.log(Object.getPrototypeOf(f) === Function.prototype); // true

And Function.prototype.constructor is of course Function.

So you are doing new Function(), which creates a function which does nothing. That useless function is called anonymous because it has no name, but it's not the function in the anonymous variable.