26
console.log(typeof String.prototype); // object
console.log(typeof Number.prototype); // object
console.log(typeof Object.prototype); // object
console.log(typeof Boolean.prototype); // object

console.log(typeof Function.prototype); // function

Why does typeof Function.prototype return "function", not "object" like other prototype objects?

Thank you!

Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
weilou
  • 4,419
  • 10
  • 43
  • 56

4 Answers4

17

This seems to be defined in ECMAScript 5:

15.3.4 Properties of the Function Prototype Object

The Function prototype object is itself a Function object (its [[Class]] is "Function") that, when invoked, accepts any arguments and returns undefined.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
leebriggs
  • 3,187
  • 1
  • 20
  • 17
  • 1
    ok but why? what's the reason behind it? Couldn't just be an object? – stackoverflow Apr 04 '17 at 11:33
  • @BarbuBarbu: look in at here: http://www.ecma-international.org/ecma-262/6.0/#sec-properties-of-the-function-prototype-object – anand Apr 05 '17 at 05:19
  • @anand so the reason, based on my conclusion is this: `"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."` ? – stackoverflow Apr 05 '17 at 06:53
  • 1
    @BarbuBarbu Why is that then ? Why did ECMAScript code that was created prior to the ECMAScript 2015 specification have `Function.prototype` as an empty function ? – doubleOrt Oct 26 '17 at 19:51
6

Its mentioned in the ECMAScript2015

http://www.ecma-international.org/ecma-262/6.0/#sec-properties-of-the-function-prototype-object :

Th 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.

This function object does not actually do anything that a function object is meant to do. You can call it with any arguments & it returns undefined. It is a dumb wrt. function object. It's a normal prototype object.

And since it's just there for compatibility reasons, it does not even has a prototype property.

For more elaboration, you can refer this answer: enter link description here

Community
  • 1
  • 1
anand
  • 695
  • 9
  • 21
1

Because function is a native object which among other properties has internal [[Construct]] and [[Call]] properties and also explicit prototype property — the reference to a prototype of the future objects. And its class is function.

F.[[Class]] = "Function"
F.[[Call]] = <reference to function> // function itself

Thus [[Call]] besides the [[Class]] property (which equals to "Function") is the main in respect of objects distinguishing. Therefore the objects having internal [[Call]] property are called as functions. The typeof operator for such objects returns "function" value.

see for reference

pradeep1991singh
  • 8,185
  • 4
  • 21
  • 31
-1

Since it has all the methods and props that any function is ought to have, this makes it effectively a function ...

Think about it for a moment and let it sink and you'll get the picture by then :)

Mr. X
  • 264
  • 3
  • 6
  • `Function.prototype` does not have `all the methods and props that any function is ought to have` quoting @traktor53 from https://stackoverflow.com/a/32929169/7206799 "Another reason to think Function.prototype is not a Function object in the usual sense (apart from saying so in the documentation) is that it cannot be called as a constructor and throws an error if an attempt is made to do so. Since the prototype property of a function is used when the function is called as a constructor, it makes sense for Function.prototype not to have this property" – Antoine Weber Oct 22 '20 at 22:29
  • though you do have a point because the logic you mention was used in ES5 time, see the very nice explanation here: https://stackoverflow.com/a/32929083/7206799 – Antoine Weber Oct 22 '20 at 22:37