[[Prototype]] vs prototype: ..what is the difference? (MyCons.__proto__ === MyCons.prototype) equals FALSE

后端 未结 3 2023
野的像风
野的像风 2020-12-05 16:52

It seems like there is a difference here...

Let\'s say we have function MyConstructor() {}

MyConstructor\'s [[Prototype]] is

相关标签:
3条回答
  • 2020-12-05 17:22

    this code will show it very clear:

    var obj = {};
    var arr = [];
    var fun = function() {};    
    
    A:     
    console.log(obj.prototype);
    console.log(arr.prototype);
    console.log(fun.prototype);
    
    B:
    console.log(arr.__proto__);
    console.log(fun.__proto__);
    
    C:
    console.log(obj.__proto__);
    console.log(arr.__proto__.__proto__);
    console.log(fun.__proto__.__proto__);
    console.log(fun.prototype.__proto__);

    the chrome console of that is:

    enter image description here

    the meaning of that:

    A. in JavaScript 'prototype' is special object that exists just in functions - for making function constructor (for your question - that's why 'prototype' it's not equal to 'proto').

    B. array and functions has the they're own 'proto' with all they're proprieties and methods that built in in JavaScript.

    C. everything in JavaScript truly inside they're objects, so the 'prototype.proto' of functions, or 'proto.proto' of functions and arrays are all equal the 'proto' of an object.

    0 讨论(0)
  • 2020-12-05 17:23

    Think of it like this. MyConstructor is a function object, so it was created by Function; therefore its [[Prototype]] (or __proto__) is identical to Function.prototype.

    In the same way, var myObj = new MyConstructor() creates an object myObj with a [[Prototype]] identical to MyConstructor.prototype.

    To put it another way, functions have a prototype property, and when you invoke functions with new, they will construct an object having a [[Prototype]] identical to the constructor function's prototype property... however a function's prototype property is not the same thing as its [[Prototype]] (or __proto__) property, because a function follows the same rules as other objects and gets its internal [[Prototype]] property from the function that constructed it (which is always Function, incidentally).


    To explain further, [[Prototype]] and prototype have entirely different purposes. [[Prototype]] is used when resolving an object's properties. If an object doesn't have a property, its [[Prototype]] is checked, and then that object's [[Prototype]], and so on, until either a property is found or you hit the end of the prototype chain.

    In contrast, prototype is the mechanism by which you assign [[Prototype]] properties to objects, since you can't access them directly other than with the non-standard __proto__ property.

    Since functions are objects, they have both a [[Prototype]] internal property, used to resolve properties as with normal objects, and a prototype property, which is assigned as the [[Prototype]] of new objects constructed by the function.

    0 讨论(0)
  • 2020-12-05 17:38

    Inspired by another answer, and to complete it:

    var obj = {name: 'Ali'};
    var arr = [1, 2, 3];
    var date = new Date();
    var func = function(){console.log('Hi!')};
    var arrowFunc = ()=>{};
    
    obj.prototype === undefined;
    arr.prototype === undefined;
    date.prototype === undefined;
    func.prototype != undefined;
    func.hasOwnProperty('prototype') === true;
    arrowFunc.prototype === undefined;
    arrowFunc.hasOwnProperty('prototype') === false;
    
    obj.__proto__ === Object.prototype;
    arr.__proto__ === Array.prototype;
    date.__proto__ === Date.prototype;
    func.__proto__ === Function.prototype;
    arrowFunc.__proto__ === Function.prototype;
    
    Array.prototype.__proto__ === Object.prototype;
    Date.prototype.__proto__ === Object.prototype;
    Function.prototype.__proto__ === Object.prototype;
    
    Object.prototype__proto__ === undefined;
    
    
    0 讨论(0)
提交回复
热议问题