F.prototype !== F.__proto__
假设您正在为所有函数设计一个 API。所以你定义每个函数都应该有方法call。你用这样的方法创建一个对象:
var fproto = {call: ()=>{}};
然后,要让所有函数共享此功能,您必须将其添加到 Function 构造函数 so that all instances of a Function inherit it 的 .prototype 属性中。因此,您执行以下操作:
Function.prototype = fproto.
现在,当您创建函数F 时,它的.__proto__ 将设置为fproto:
const F = new Function();
F.call(); // works because of lookup in prototype chain through `__proto__` property
F.__proto__ === Function.prototype; // true
现在您决定使用F 构造函数创建的所有实例都应该有一个方法custom,因此您使用该属性创建一个对象iproto,并将其设置为使用@ 的所有F 实例的原型987654335@财产:
const iproto = {custom: ()=>{}};
F.prototype = iproto;
const myobj = new F();
myobj.custom(); // works
所以现在应该清楚F.__proto__ 和F.prototype 不是同一个对象。当你声明一个函数时,这基本上就是在幕后发生的事情:
const F = function() {};
// F.__proto__ is set to Function.prototype to inherit `call` and other methods
F.__proto__ === Function.prototype
// F.prototype is set to a new object `{constructor: F}` and so:
F.prototype !== Function.prototype
Function.prototype === Function.__proto__
这是一个例外情况,因为Function 构造函数应该具有可用于函数实例的所有方法,因此Function.__proto__,但所有这些方法都与函数实例共享这些方法,因此Function.prototype。