In JavaScript, why typeof Function.prototype is “function”, not “object” like other prototype objects?

前端 未结 4 519
天涯浪人
天涯浪人 2020-11-27 07:05
console.log(typeof String.prototype); // object
console.log(typeof Number.prototype); // object
console.log(typeof Object.prototype); // object
console.log(typeof Bo         


        
相关标签:
4条回答
  • 2020-11-27 07:40

    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

    0 讨论(0)
  • 2020-11-27 07:44

    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.

    0 讨论(0)
  • 2020-11-27 07:57

    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 :)

    0 讨论(0)
  • 2020-11-27 07:58

    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

    0 讨论(0)
提交回复
热议问题