-4

I learnt that any function type object has property prototype.

For example:

  • Object has property prototype

  • Function has property prototype

  • Person has property prototype

But,

> typeof Function.prototype

"function"

I have two questions,

1) why function type object Function.prototype does not have its own property prototype, in the below visualisation?

2) Any object usually inherits from an object type object, but in the below visualization, Function object and Person object inherit from function type object Function.prototype?

So, For above two questions, Is it more safe to have Function.protoype as object type?

enter image description here

overexchange
  • 15,768
  • 30
  • 152
  • 347

2 Answers2

3

Why function type object Function.prototype does not have its own property prototype, in the below visualisation?

Why would it, what would you expect it to point to? It's not a constructor function. (In fact, with ES6, many functions don't have a .prototype method).

It's rather quite weird that Function.prototype is a callable object (function), there's actually no benefit from that. It could've been a plain object just as well. "The spec said so" is all we have (see also these three questions).

Any object usually inherits from an object type object, but the Function object and Person object inherit from function type object Function.prototype?

Well yes, why wouldn't they? As you say yourself, functions are just objects, and every object inherits from another object (or null). That some objects are callable (and typeof yields "function" for them) does not make any difference for inheritance.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375
1

If you compare the prototypes of the common types in JavaScript you can see that their prototype is an instance of the actual type:

Function.prototype = function () {}
Array.prototype = []
Object.prototype = {}

I am not sure why that is, but i guess that is an ECMA-standard or it is a thing which Browser implement for themself so everything makes sense. In other words how would you know a Function is a Function when the prototype of Function is an object like the prototype of Object. English isn't my mother tongue but I hope I made my point clear.

But to answer your question: I guess the prototype of Function is an exception and has no own prototype, because it is not needed.

DavidVollmers
  • 677
  • 5
  • 17
  • [Here's a relevant part of the spec](http://www.ecma-international.org/ecma-262/5.1/#sec-15.3.3.1): _"The initial value of Function.prototype is the standard built-in Function prototype object"_ – James Thorpe Dec 03 '15 at 11:38
  • @JamesThorpe Is it more safe to have `Function.prototype` as `object` type object, to resolve my two questions? Are there any issues in doing that? – overexchange Dec 03 '15 at 12:05