What is the end of prototype chain in javascript — null or Object.prototype?

前端 未结 3 1877
猫巷女王i
猫巷女王i 2021-01-05 22:39

I\'ve been reading about prototype chain in JavaScript and came to two slightly different definitions. It is said that every object in JavaScript has a prototype and that pr

3条回答
  •  时光取名叫无心
    2021-01-05 23:32

    What is the end of prototype chain in javascript

    Null. The only authority on the language is ECMA-262.

    Are objects, which are not created from object literals, not linked to Object.prototype

    They may or many not be, e.g.

    var x = Object.create(null)
    

    has a [[Prototype]] of null, whereas:

    var y = {};
    

    has a [[Prototype]] of Object.prototype.

    Say I have an object var x = { len: 4, breadth: 5}. Would javascript automatically create it's proptotype x.prototype.

    No. Function objects have default prototype objects. Plain objects have a default [[Prototype]] (i.e. internal prototype property) that is Object.prototype (unless constructed as above).

    And how long would the prototype chain be? Would x.prototype have only one prototype Object.prototype making a 3 point chain?

    Two values: Object.prototype and null.

    How does javascript internally creates automatic prototypes?

    However it likes, the language specification does not define implementation, only behaviour.

提交回复
热议问题