普通函数和箭头函数在 _proto_ 构造函数方面有何不同?
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());
两个函数 return 当我尝试创建它的新实例时,名称为 anonymous
的函数。
我的问题:当我调用anonymous()
函数时,为什么它没有命中第二个构造函数(箭头函数)?
两个函数的[[Prototype]]都是Function.prototype
。从这个意义上说,函数表达式和箭头函数没有区别。
let anonymous = function () {
return 'hello'
};
let f = () => 'world';
console.log(Object.getPrototypeOf(anonymous) === Function.prototype); // true
console.log(Object.getPrototypeOf(f) === Function.prototype); // true
而Function.prototype.constructor
当然是Function
。
所以你正在做 new Function()
,它创建了一个什么都不做的函数。那个无用的函数被称为 anonymous
因为它没有名字,但它不是 anonymous
变量中的函数。
let anonymous = function () {
return 'hello'
};
let f = () => 'world';
console.log(new anonymous['__proto__'].constructor());
console.log(new f['__proto__'].constructor());
console.log(anonymous());
两个函数 return 当我尝试创建它的新实例时,名称为 anonymous
的函数。
我的问题:当我调用anonymous()
函数时,为什么它没有命中第二个构造函数(箭头函数)?
两个函数的[[Prototype]]都是Function.prototype
。从这个意义上说,函数表达式和箭头函数没有区别。
let anonymous = function () {
return 'hello'
};
let f = () => 'world';
console.log(Object.getPrototypeOf(anonymous) === Function.prototype); // true
console.log(Object.getPrototypeOf(f) === Function.prototype); // true
而Function.prototype.constructor
当然是Function
。
所以你正在做 new Function()
,它创建了一个什么都不做的函数。那个无用的函数被称为 anonymous
因为它没有名字,但它不是 anonymous
变量中的函数。