__proto__ VS. prototype in JavaScript

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

    prototype

    prototype is a property of a Function. It is the blueprint for creating objects by using that (constructor) function with new keyword.

    __proto__

    __proto__ is used in the lookup chain to resolve methods, properties. when an object is created (using constructor function with new keyword), __proto__ is set to (Constructor) Function.prototype

    function Robot(name) {
        this.name = name;
    }
    var robot = new Robot();
    
    // the following are true   
    robot.__proto__ == Robot.prototype
    robot.__proto__.__proto__ == Object.prototype
    

    Here is my (imaginary) explanation to clear the confusion:

    Imagine there is an imaginary class (blueprint/coockie cutter) associated with function. That imaginary class is used to instantiate objects. prototype is the extention mechanism (extention method in C#, or Swift Extension) to add things to that imaginary class.

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

    The above can be imagined as:

    // imaginary class
    class Robot extends Object{
    
        static prototype = Robot.class  
        // Robot.prototype is the way to add things to Robot class
        // since Robot extends Object, therefore Robot.prototype.__proto__ == Object.prototype
    
        var __proto__;
    
        var name = "";
    
        // constructor
        function Robot(name) {
    
            this.__proto__ = prototype;
            prototype = undefined;
    
            this.name = name;
        }
    
    } 
    

    So,

    var robot = new Robot();
    
    robot.__proto__ == Robot.prototype
    robot.prototype == undefined
    robot.__proto__.__proto__ == Object.prototype
    

    Now adding method to the prototype of Robot:

    Robot.prototype.move(x, y) = function(x, y){ Robot.position.x = x; Robot.position.y = y};
    // Robot.prototype.move(x, y) ===(imagining)===> Robot.class.move(x, y)
    

    The above can be imagined as extension of Robot class:

    // Swift way of extention
    extension Robot{
        function move(x, y){    
            Robot.position.x = x; Robot.position.y = y
        }
    }
    

    Which in turn,

    // imaginary class
    class Robot{
    
        static prototype = Robot.class // Robot.prototype way to extend Robot class
        var __proto__;
    
        var name = "";
    
        // constructor
        function Robot(name) {
    
            this.__proto__ = prototype;
            prototype = undefined;
    
            this.name = name;
        }
    
        // added by prototype (as like C# extension method)
        function move(x, y){ 
            Robot.position.x = x; Robot.position.y = y
        };
    }
    

提交回复
热议问题