__proto__ VS. prototype in JavaScript

后端 未结 30 2501
爱一瞬间的悲伤
爱一瞬间的悲伤 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:35

    Every function you create has a property called prototype, and it starts off its life as an empty object. This property is of no use until you use this function as constructor function i.e. with the 'new' keyword.

    This is often confused with the __proto__ property of an object. Some might get confused and except that the prototype property of an object might get them the proto of an object. But this is not case. prototype is used to get the __proto__ of an object created from a function constructor.

    In the above example:

    function Person(name){
        this.name = name
    }; 
    
    var eve = new Person("Eve");
    
    console.log(eve.__proto__ == Person.prototype) // true
    // this is exactly what prototype does, made Person.prototype equal to eve.__proto__

    I hope it makes sense.

提交回复
热议问题