Function.prototype是一个函数 [英] Function.prototype is a function

查看:265
本文介绍了Function.prototype是一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在深入研究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:


  1. 是否有理由 Function.prototype 是类型函数,而不是对象?

    typeof Function.prototype; //function

  2. Function.prototype JS中的'unique function',因为它没有像其他函数一样拥有自己的原型属性吗? (是否有一个普遍接受的'名称'来引用它?)

  1. Is there a reason for Function.prototype to be of type function, instead of object?
    typeof Function.prototype; //"function"
  2. 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 is an Object object.
  • Function.prototype is a Function object which returns undefined 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 is false.
  • Number.prototype is a Number object whose value is +0.
  • Date.prototype is a Date object whose [[PrimitiveValue]] is NaN.
  • RegExp.prototype is a RegExp object whose data properties are like new 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 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 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 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屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆