Java Reference
In-Depth Information
<< 2;
typeof Number(2); // but it's actually an object!
<< "object"
Similarly, primitive values are not instances of these constructor functions:
2 instanceof Number;
<< false
In fact, the two things are not strictly equal:
Number(2) === 2;
<< false
Primitives are actually without their own methods. The primitive wrapper
objects Number , String , and Boolean are used in the background to
provide primitive values with methods. When a method is called on a prim-
itive value, JavaScript creates a wrapper object for the primitive, which con-
verts it into an object and then calls the method on the object. This means
that it is possible to call methods on primitives such as we saw in Chapter 2:
2..toExponential(); // remember 2 dots to call
methods on
integers!
<< "2e+0"
In the background, something similar to this is happening:
new Number(2).toExponential();
<< "2e+0"
Even custom objects that we've created have a toString() method:
mike.toString();
<< "[object Object]"
It may convey little information, but it does return a string representation of the object.
Search WWH ::




Custom Search