__proto__ VS. prototype in JavaScript

后端 未结 30 1897
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-21 06:14

This figure again shows that every object has a prototype. Constructor function Foo also has its own __proto__ which is Function.prototype, a

相关标签:
30条回答
  • 2020-11-21 06:37

    As this rightly stated

    __proto__ is the actual object that is used in the lookup chain to resolve methods, etc. prototype is the object that is used to build __proto__ when you create an object with new:

    ( new Foo ).__proto__ === Foo.prototype;
    ( new Foo ).prototype === undefined;
    

    We can further note that __proto__ property of an object created using function constructor points towards the memory location pointed towards by prototype property of that respective constructor.

    If we change the memory location of prototype of constructor function, __proto__ of derived object will still continue to point towards the original address space. Therefore to make common property available down the inheritance chain, always append property to constructor function prototype, instead of re-initializing it (which would change its memory address).

    Consider the following example:

    function Human(){
        this.speed = 25;
    }
    
    var himansh = new Human();
    
    Human.prototype.showSpeed = function(){
        return this.speed;
    }
    
    himansh.__proto__ === Human.prototype;  //true
    himansh.showSpeed();    //25
    
    //now re-initialzing the Human.prototype aka changing its memory location
    Human.prototype = {lhs: 2, rhs:3}
    
    //himansh.__proto__ will still continue to point towards the same original memory location. 
    
    himansh.__proto__ === Human.prototype;  //false
    himansh.showSpeed();    //25
    
    0 讨论(0)
  • 2020-11-21 06:38

    my understanding is: __proto__ and prototype are all served for the prototype chain technique . the difference is functions named with underscore(like __proto__) are not aim for developers invoked explicitly at all. in other words, they are just for some mechanisms like inherit etc. they are 'back-end'. but functions named without underscore are designed for invoked explicitly, they are 'front-end'.

    0 讨论(0)
  • 2020-11-21 06:40

    To explain let us create a function

     function a (name) {
      this.name = name;
     }
    

    When JavaScript executes this code, it adds prototype property to a, prototype property is an object with two properties to it:

    1. constructor
    2. __proto__

    So when we do

    a.prototype it returns

         constructor: a  // function definition
        __proto__: Object
    

    Now as you can see constructor is nothing but the function a itself and __proto__ points to the root level Object of JavaScript.

    Let us see what happens when we use a function with new key word.

    var b = new a ('JavaScript');
    

    When JavaScript executes this code it does 4 things:

    1. It creates a new object, an empty object // {}
    2. It creates __proto__ on b and makes it point to a.prototype so b.__proto__ === a.prototype
    3. It executes a.prototype.constructor (which is definition of function a ) with the newly created object (created in step#1) as its context (this), hence the name property passed as 'JavaScript' (which is added to this) gets added to newly created object.
    4. It returns newly created object in (created in step#1) so var b gets assigned to newly created object.

    Now if we add a.prototype.car = "BMW" and do b.car, the output "BMW" appears.

    this is because when JavaScript executed this code it searched for car property on b, it did not find then JavaScript used b.__proto__ (which was made to point to 'a.prototype' in step#2) and finds car property so return "BMW".

    0 讨论(0)
  • 2020-11-21 06:41

    To make it a little bit clear in addition to above great answers:

    function Person(name){
        this.name = name
     }; 
    
    var eve = new Person("Eve");
    
    eve.__proto__ == Person.prototype //true
    
    eve.prototype  //undefined
    

    Instances have __proto__, classes have prototype.

    0 讨论(0)
  • 2020-11-21 06:43

    prototype is a property of a Function object. It is the prototype of objects constructed by that function.

    __proto__ is internal property of an object, pointing to its prototype. Current standards provide an equivalent Object.getPrototypeOf(O) method, though de facto standard __proto__ is quicker.

    You can find instanceof relationships by comparing a function's prototype to an object's __proto__ chain, and you can break these relationships by changing prototype.

    function Point(x, y) {
        this.x = x;
        this.y = y;
    }
    
    var myPoint = new Point();
    
    // the following are all true
    myPoint.__proto__ == Point.prototype
    myPoint.__proto__.__proto__ == Object.prototype
    myPoint instanceof Point;
    myPoint instanceof Object;
    

    Here Point is a constructor function, it builds an object (data structure) procedurally. myPoint is an object constructed by Point() so Point.prototype gets saved to myPoint.__proto__ at that time.

    0 讨论(0)
  • 2020-11-21 06:46

    __proto__ is the base to construct prototype and a constructor function eg: function human(){} has prototype which is shared via __proto__ in the new instance of the constructor function. A more detailed read here

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