difference between Object.create(Object.prototype) , Object.create(Object) and Object.create(null)

前端 未结 2 1423
既然无缘
既然无缘 2020-12-24 14:48

Which parameter should i pass for the first parent object from which others will inherit and which one is more efficient

Object.create(Object.prototype)

Obj         


        
2条回答
  •  忘掉有多难
    2020-12-24 15:08

    What is being returned is an object.

    >>> typeof Object.create(Object)
    <<< "object"
    >>> Object.create(Object)
    <<< Function {}
    //           ^^
    

    Function is the name which Chrome addresses the object's constructor. See How are javascript class names calculated for custom classes in Chrome Dev Tools?


    This part of the answer addresses @phenomnomnominal's comment in the question, explaining why the created object has inherits function properties such as call.

    The Object constructor is a function, and thus inherits from the Function prototype:

    >>> Object.call === Function.prototype.call
    <<< true
    

    So an object having Object as prototype will have a link to the Function prototype via prototype chain as well:

    >>> Object.create(Object).call === Function.prototype.call
    <<< true
    

    And as mentioned by @TJ, using a constructor as prototype is rather odd. You should specify an object as the prototype that the created object will inherit from. @TJ already did a pretty good job explaining this part.

提交回复
热议问题