Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)?

like image 734
Tân Avatar asked Jun 14 '26 18:06

Tân


1 Answers

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.

like image 182
Oriol Avatar answered Jun 17 '26 07:06

Oriol



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!