search

Home  >  Q&A  >  body text

javascript - Prototype chain problem of Object and Function

The above is my test in the browser. I am confused. I can’t understand this prototype chain at all. Can anyone help explain

仅有的幸福仅有的幸福2747 days ago526

reply all(1)I'll reply

  • 滿天的星座

    滿天的星座2017-05-19 10:13:54

    Object is a function, don’t be misled by its name, so complete the form in your mind first

    function Object () {
      ...
    }
    

    Any function (constructor) has a prototype, which is prototype. There is nothing special about prototype, it is just an object.

    Object.prototype outputs “Object { ... }”, don’t be misled by this output and think that Object.prototype is itself. The "Object" in the output simply means that the value of Object.prototype "is an object of type "Object".

    But at the same time, because function itself in JS is also an object, Object is not only a function, but also an object. All objects are "constructed" (initialized) based on a prototype.

    When is Object constructed as an object?

    A: When it is defined, that is:

    function Object() {
      ...
    }
    

    At this time, the JS runtime constructs the function "object" (instance) of Object based on function () { [Native code] } as the prototype.

    What is this function () { [Native code] }? It is the ancestor of all functions in JS.

    Object.__proto__.__proto__ is the prototype of this ancestor function. Since it is an ancestor, how can it have a prototype? Remember that it is the ancestor of the function, but it is not the ancestor of the "object". The ancestor of the object is this "Object {__defineGetter__: ...}".

    You asked again, since objects are constructed, then ancestor objects should also be constructed, right? The ancestor object is the origin of all things. It is defined by the language designer of JS and is the starting point of the conceptual system.

    But I saw

    Object.prototype.constructor
    function Object() { [native code] }
    

    The ancestor object is not clearly constructed? And it is constructed from Object?

    This is just that the JS language designer pointed the constructor of the ancestor object to Object for the sake of conceptual consistency.

    What was the ancestor object before?

    Object.prototype.__proto__
    null
    

    Tao gives birth to one, and gives birth to two.
    The Tao is nothingness.

    itlr.cc

    reply
    0
  • Cancelreply