Home  >  Q&A  >  body text

javascript - How to understand the difference between prototype and __proto__?

How to understand the difference between prototype and __proto__?

漂亮男人漂亮男人2641 days ago1065

reply all(5)I'll reply

  • 迷茫

    迷茫2017-06-30 10:01:49

    When the constructor accesses the prototype, it is usually accessed through prototype. For example, we add methods to the prototype

    Person.prototype.getName = function() {}

    When the instance out of new accesses the prototype, in some supported browsers

    function Person() {}
    
    var p1 = new Person();
    
    p1.__proto__ === Person.prototype   // true

    Conclusion: prototype is used as a constructor to access the prototype, and __proto__ is used as an instance to access the prototype. When their identities are different, even when a method calls both at the same time, different prototypes may be accessed.

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-06-30 10:01:49

    Every object has __proto__, and prototype only Function has;

    reply
    0
  • 漂亮男人

    漂亮男人2017-06-30 10:01:49

    Maybe you can also check out these:

    /a/11...

    https://developer.mozilla.org...

    https://developer.mozilla.org...

    reply
    0
  • 某草草

    某草草2017-06-30 10:01:49

    prototype is an attribute of the constructor, __proto__ is an attribute of the instance. The __proto__ attribute of an instance generated using a constructor will point to the object pointed to by the constructor's prototype attribute.

    Well, that’s it.

    reply
    0
  • 伊谢尔伦

    伊谢尔伦2017-06-30 10:01:49

    In terms of function:
    prototype determines the default value of proto when an object is defined using a constructor or literal form

    proto is the basis for js engine prototype chain search

    So the key point is that when you want to change the search method of the prototype chain, you can change the prototype chain search method of all subsequent instantiated objects by changing the constructor prototype, and use proto to modify the prototype chain search of a single object.

    reply
    0
  • Cancelreply