Constructor.prototype not in the prototype chain?

后端 未结 2 991
广开言路
广开言路 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:17

    a = 12 creates a primitive number, which is not quite the same as a Number object. Primitives are implicitly cast to objects for purposes of property access.

    a = 12; //a is a primitive
    b = new Number(12); //b is an object
    a.constructor.prototype.isPrototypeOf(a); //false because a is primitive
    b.constructor.prototype.isPrototypeOf(b); //true because b is an object
    

    As per the ECMAScript spec:

    When the isPrototypeOf method is called with argument V, the following steps are taken:

    1. If V is not an object, return false.

    primitive numbers are not, strictly speaking, objects.

提交回复
热议问题