Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 为什么F-简单函数:F.prototype!==但是Function.prototype==函数?_Javascript_Oop_Prototype_Proto_Function.prototype - Fatal编程技术网

Javascript 为什么F-简单函数:F.prototype!==但是Function.prototype==函数?

Javascript 为什么F-简单函数:F.prototype!==但是Function.prototype==函数?,javascript,oop,prototype,proto,function.prototype,Javascript,Oop,Prototype,Proto,Function.prototype,为什么如果F-简单功能: F.prototype !== F.__proto__ 但是 ? F.prototype!==F.uuu协议uuuu 假设您正在为所有函数设计一个API。因此,您定义了每个函数都应该有方法调用。使用以下方法创建对象: var fproto = {call: ()=>{}}; 然后,要使所有函数共享此功能,必须将其添加到函数构造函数的.prototype属性中。因此,您可以执行以下操作: Function.prototype = fproto. 现在,当您创建

为什么如果
F
-简单功能:

F.prototype !== F.__proto__
但是

?

F.prototype!==F.uuu协议uuuu

假设您正在为所有函数设计一个API。因此,您定义了每个函数都应该有方法
调用
。使用以下方法创建对象:

var fproto = {call: ()=>{}};
然后,要使所有函数共享此功能,必须将其添加到函数构造函数的
.prototype
属性中。因此,您可以执行以下操作:

Function.prototype = fproto.
现在,当您创建函数
F
时,它的
\uuuu proto\uuuu
将设置为
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
,并使用
prototype
属性将其设置为
F
的所有实例的原型:

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

const myobj = new F();
myobj.custom(); // works
所以现在应该清楚的是,
F.\uu proto\uu
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==函数


是一种例外情况,因为
函数
构造函数应具有可用于函数实例的所有方法,因此
函数.\uuuu proto\uuuu
,但所有构造函数都与函数实例共享这些方法,因此
Function.prototype

Function.prototype==Function.\uuuu proto\uu因为
Function
是一个特殊的雪花?你为什么期望它不是这样?您还希望它引用什么?
\uuuu proto\uuuu
已弃用,您不应再使用它。请使用
Object.getPrototypeOf(…)
取而代之,它是这样定义的:“函数构造函数的
[[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