Function.prototype是一个函数 [英] Function.prototype is a function
问题描述
我正在深入研究Javascript原型链。
为了记录我的发现,我得出了以下方案:
I'm digging into the Javascript prototype chain.
In order to document my findings, I've drawn the following scheme:
虽然大多数概念都很清楚,但我只剩下两个相关问题。而不是将它们分开,我猜想在这个问题中集中它们可能会更好:
Although most of the concepts are clear, I'm left with just two related questions. Rather than splitting them up, I guessed that centralising them in this question might be better:
- 是否有理由
Function.prototype
是类型函数,而不是对象?
typeof Function.prototype; //function
- 是
Function.prototype
JS中的'unique function',因为它没有像其他函数一样拥有自己的原型属性吗? (是否有一个普遍接受的'名称'来引用它?)
- Is there a reason for
Function.prototype
to be of type function, instead of object?
typeof Function.prototype; //"function"
- Is
Function.prototype
a 'unique function' in JS since it doesn't have a prototype property of its own like other functions do? (is there a generally accepted 'name' to refer to it?)
推荐答案
原因 ES5规范是这样说的:
函数原型对象本身就是一个Function对象(它的
[[Class]]是Function),当被调用时,接受任何参数,
返回undefined。
The Function prototype object is itself a Function object (its [[Class]] is "Function") that, when invoked, accepts any arguments and returns undefined.
注意在ES5中通常使某些类的原型成为该成员的一部分class:
Note it's common in ES5 to make the prototype of some class a member of that class:
-
Object.prototype
是一个Object对象。 -
Function.prototype
是一个Function对象,在调用时返回undefined
。 -
Array.prototype
是一个空的Array对象。 -
String.prototype
是一个String对象,其值为空String。 -
Boolean.prototype
是一个布尔对象,其值是false
。 -
Number.prototype
是一个Number对象,其值为+0
。 -
Date.prototype
是一个Date对象,其[[PrimitiveValue]]是NaN
。 -
RegExp.prototype
是一个RegExp对象,其数据属性类似于new RegExp()
的那些。 -
Error.prototype
是一个Error对象。
Object.prototype
is an Object object.Function.prototype
is a Function object which returnsundefined
when invoked.Array.prototype
is an empty Array object.String.prototype
is a String object whose value is an empty String.Boolean.prototype
is a Boolean object whose value isfalse
.Number.prototype
is a Number object whose value is+0
.Date.prototype
is a Date object whose [[PrimitiveValue]] isNaN
.RegExp.prototype
is a RegExp object whose data properties are likenew RegExp()
's ones.Error.prototype
is an Error object.
我认为它是标准化的,因为类的原型具有内在的该类的属性,作为该类的实例。如果看起来像鸭子,它应该像鸭子一样。因此,在原型本身而不是在实例上调用原型的方法也应该有效。
I think it was standardized as such because the prototype of a class has the intrinsic properties of that class, as the instances of that class. And if it looks like a duck it should behave as a duck. So calling the methods of the prototype on the prototype itself instead of on an instance should work too.
然而,ES6并不喜欢这样。所以它改变了那些行为:
However, ES6 didn't like this. So it changed the behavior for those:
-
Boolean.prototype
是一个没有的普通对象[[BooleanData]]内部插槽。 -
Error.prototype
是一个没有[[ErrorData]]内部插槽的普通对象。 -
Number.prototype
是一个没有[[NumberData]]内部插槽的普通对象。 -
Date.prototype
是一个没有[[DateValue]]内部插槽的普通对象。 -
String.prototype
是一个没有[[StringData]]内部插槽的普通对象。 -
RegExp.prototype
是一个普通的对象,没有[[ RegExpMatcher]]也不是RegExp实例对象的任何其他内部插槽。
Boolean.prototype
is an ordinary object with no [[BooleanData]] internal slot.Error.prototype
is an ordinary object with no [[ErrorData]] internal slot.Number.prototype
is an ordinary object with no [[NumberData]] internal slot.Date.prototype
is an ordinary object with no [[DateValue]] internal slot.String.prototype
is an ordinary object with no [[StringData]] internal slot.RegExp.prototype
is an ordinary object with no [[RegExpMatcher]] nor any of the other internal slots of RegExp instance objects.
还有新的类 (ES6对象不再具有[[Class]]) :
And also for new "classes" (ES6 objects no longer have a [[Class]]):
-
Symbol.prototype
是一个没有[[SymbolData]]内部插槽的普通对象。 -
TypedArray.prototyp e
是一个普通对象,没有[[ViewedArrayBuffer]],也没有任何其他特定于 TypedArray 实例对象的内部插槽。 -
Map.prototype
是一个没有[[MapData]]内部插槽的普通对象。 -
Set.prototype
是一个没有[[SetData]]内部插槽的普通对象。 -
WeakMap.prototype
是一个没有[[WeakMapData]]内部插槽的普通对象。 -
WeakSet.prototype
是一个没有[[WeakSetData]]内部插槽的普通对象。 -
ArrayBuffer.prototype
是一个没有[[ArrayBufferData]的普通对象]]和[[ArrayBufferByteLength]]内部插槽。 -
DataView.prototype
是一个没有[[DataView]]的普通对象,[[ ViewedArrayBuffer]],[[ByteLength]],[[ByteOffset]]内部插槽。 -
GeneratorFunction.prototype
是一个没有的普通对象[[ECMAScriptCode]]或表27 或表56 。 -
Promise.prototype
是一个没有[[PromiseState]]的普通对象,也不是Promise实例的任何其他内部插槽。
Symbol.prototype
is an ordinary object with no [[SymbolData]] internal slot.TypedArray.prototype
is an ordinary object with no [[ViewedArrayBuffer]] nor any other of the internal slots that are specific to TypedArray instance objects.Map.prototype
is an ordinary object with no [[MapData]] internal slot.Set.prototype
is an ordinary object with no [[SetData]] internal slot.WeakMap.prototype
is an ordinary object with no [[WeakMapData]] internal slot.WeakSet.prototype
is an ordinary object with no [[WeakSetData]] internal slot.ArrayBuffer.prototype
is an ordinary object with no [[ArrayBufferData]] nor [[ArrayBufferByteLength]] internal slots.DataView.prototype
is an ordinary object with no [[DataView]], [[ViewedArrayBuffer]], [[ByteLength]], nor [[ByteOffset]] internal slots.GeneratorFunction.prototype
is an ordinary object with no [[ECMAScriptCode]] nor any other of the internal slots listed in Table 27 or Table 56.Promise.prototype
is an ordinary object with no [[PromiseState]] nor any of the other internal slots of Promise instances.
但旧的行为仍然适用于:
However, the old behavior remains for those:
-
Function.prototype
本身就是一个内置函数对象。 -
Array.prototype
是一个Array异物并且具有为这些对象指定的内部方法。
Function.prototype
is itself a built-in function object.Array.prototype
is an Array exotic object and has the internal methods specified for such objects.
所以现在的原因是backwa rds兼容性:
So now the reason is backwards compatibility:
将Function原型对象指定为
的函数对象,确保与之前创建的ECMAScript代码兼容到
ECMAScript 2015规范。
The Function prototype object is specified to be a function object to ensure compatibility with ECMAScript code that was created prior to the ECMAScript 2015 specification.
注意这不会使 Function.prototype
一个特殊功能。只有构造函数具有 prototype
property :
Note this doesn't make Function.prototype
a special function. Only constructors have the prototype
property:
可用作构造函数的函数实例具有
prototype
property。
Function instances that can be used as a constructor have a
prototype
property.
除了<之外,还有多个非构造函数的例子code> Function.prototype ,例如
-
数学
对象:
typeof Math.pow; // "function
'prototype' in Math.pow; // false
一些主机对象:
Some host objects:
typeof document.createElement('object'); // "function
'prototype' in document.createElement('object'); // false
在ES6中,箭头功能:
In ES6, arrow functions:
typeof (x => x * x); // "function
'prototype' in (x => x * x); // false
这篇关于Function.prototype是一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!