本文介绍了为什么如果 F - 简单函数:F.prototype!== F.__proto__ 但 Function.prototype === Function.__proto__?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么如果 F - 简单的函数:

F.prototype !== F.__proto__

但是

Function.prototype === Function.__proto__

?

解决方案

假设您正在为所有功能设计 API.所以你定义了每个函数都应该有方法call.你用这样的方法创建一个对象:

var fproto = {call: ()=>{}};

然后为了让所有函数共享这个功能,你必须将它添加到函数构造函数的 .prototype 属性中,以便函数的所有实例都继承它.因此,您执行以下操作:

Function.prototype = fproto.

现在,当您创建一个函数 F 时,它会将其 .__proto__ 设置为 fproto:

const F = new Function();F.call();//因为通过 `__proto__` 属性在原型链中查找而起作用F.__proto__ === Function.prototype;//真的

现在您决定使用 F 构造函数创建的所有实例都应该有一个方法 custom,因此您创建了一个具有属性的对象 iproto并使用 prototype 属性将其设置为 F 的所有实例的原型:

const iproto = {custom: ()=>{}};F.prototype = iproto;const myobj = new F();myobj.custom();//有效

所以现在应该清楚 F.__proto__F.prototype 不是同一个对象.当你声明一个函数时,这基本上是发生在引擎盖下的事情:

const F = function() {};//F.__proto__ 设置为Function.prototype 继承`call`等方法F.__proto__ === Function.prototype//F.prototype 被设置为一个新对象 `{constructor: F}` 等等:F.prototype !== Function.prototype

Function.prototype === Function.__proto__

是一个例外情况,因为Function 构造函数应该拥有所有可用于函数实例的方法,因此 Function.__proto__,但都与函数实例共享这些方法,因此 Function.prototype.

Why if F - simple function:

F.prototype !== F.__proto__

but

Function.prototype === Function.__proto__

?

解决方案

Suppose you're designing an API for all functions. So you define that every function should have the method call. You create an object with such method:

var fproto = {call: ()=>{}};

Then for all functions to share this functionality, you have to add it to .prototype property of a Function constructor, so that all instances of a Function inherit it. So you do the following:

Function.prototype = fproto.

Now, when you create a function F, it will have have its .__proto__ set to fproto:

const F = new Function();
F.call(); // works because of lookup in prototype chain through `__proto__` property
F.__proto__ === Function.prototype; // true

Now you decide that all instances created using F constructor, should have a method custom, so you create an object iproto with the property and set it as a prototype for all instances of F using prototype property:

const iproto = {custom: ()=>{}};
F.prototype = iproto;

const myobj = new F();
myobj.custom(); // works

So now it should be clear that F.__proto__ and F.prototype are not the same object. And this is essentially what happens under the hood when you declare a function:

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

Is an exceptional case because Function constructor should have all methods available for function instances, and hence Function.__proto__, but all share these methods with function instances, hence Function.prototype.

这篇关于为什么如果 F - 简单函数:F.prototype!== F.__proto__ 但 Function.prototype === Function.__proto__?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 04:41