Function.prototype is a function

后端 未结 3 1379
粉色の甜心
粉色の甜心 2020-12-01 04:08

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

3条回答
  •  余生分开走
    2020-12-01 04:33

    In answer to your questions:

    1) Function.prototype is a type of function because, according to ECMAScript 2015:

    The Function prototype object is the intrinsic object %FunctionPrototype%. The Function prototype object is itself a built-in function object.

    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.

    So the Function prototype object is only defined as a Function object to ensure compatability with older ECMAScript standards. The function doesn't actually do anything:

    When invoked, it accepts any arguments and returns undefined.

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

    2) Regarding the prototype property:

    The Function prototype object does not have a prototype property.

    Same Source

    This is unique since all functions usually possess a prototype property, however since the Function prototype object is only specified as a Function object to maintain compatability, it's behaviour is unlike that of regular functions.

    I've created a JSFiddle with various tests in case it helps anyone:

    http://jsfiddle.net/Ld0b39xz/

    // We'll use 'Object.getPrototypeOf' to access [[prototype]]
    
    // As you know, [[prototype]] of Object.prototype returns 'null'. 
    console.log(Object.getPrototypeOf(Object.prototype));
    // null
    
    ////////////////////////////////////////////////////////
    
    // Let's take a closer look at Function.prototype
    console.log(Function.prototype);
    // Output:
    // function(){}
    
    // This is what the specs say should happen:
    // "The Function prototype object is itself a built-in function object."
    
    /////////////////////////////////////////////////////
    
    // Let's see if this function has a 'prototype' property.
    // All functions normally have a prototype property that initially
    // references an empty object...except this one.
    var fn = Function.prototype;
    console.log(fn.prototype);
    // Output:
    // undefined
    
    // This is expected, according to the specs:
    // "The Function prototype object does not have a prototype property."
    
    // It does have some properties such as 'name' and 'length',
    // but not 'prototype'.
    
    ////////////////////////////////////////////////////////
    
    // Let's see what [[prototype]] of Function.prototype returns.
    console.log(Object.getPrototypeOf(Function.prototype));
    // Output:
    // Object{}
    
    // Again this is expected:
    // "The value of the [[Prototype]] internal slot of the
    // Function prototype object is the intrinsic object %ObjectPrototype%"
    
    /////////////////////////////////////////////////////////
    
    // Now lets see what the [[Prototype]] of this object is:
    console.log(Object.getPrototypeOf(Object.getPrototypeOf(Function.prototype)));
    // Output:
    // null
    
    // We've come full circle since all the statement above is
    // doing is looking for the prototoype of the native Object,
    // which we already know is 'null' from our first test.
    

提交回复
热议问题