Constructor.prototype not in the prototype chain?

后端 未结 2 1021
广开言路
广开言路 2021-01-18 04:28

related: Confusion about protype chain , primitives and objects

in Firebug console :

a = 12
a.constructor.prototype.isPrototypeOf(a) // prints \'fals         


        
2条回答
  •  情书的邮戳
    2021-01-18 05:06

    a = new Number(12);
    a.constructor.prototype.isPrototypeOf(a) // prints 'true'
    

    I'm not smart enough to tell you why I just know that this is how it is. And yes, it's weird.

    Now, you could say "12 is a primitive and new Number(12) is an object". But how do you explain this?

    (12).toFixed(3); // "12.000"
    

    Apparently somewhere JavaScript is deciding the primitive might as well be an object.

    Why does this distinction exist? How do you convert between the two forms? How does this impact performance? All questions related to this question that I don't have the answer to.

提交回复
热议问题