So, there are three primitive wrapper types and they are: 1:
Number and 3:
Boolean. And these primitive wrapper types are reference types that are automatically created behind the scenes whenever these primitive wrappers types are read. Let’s consider an example to explain our point.
var name = "Lorem"; var firstChar = name.charAt(0); console.log(firstChar); // "L"
In the first line of the above code, a primitive string value has been assigned to
name. The second line treats
name like an object and calls
charAt(0) using dot notation.
Now let us examine behind the scenes of the above code:
Sting so that
charAt(0) will work. The
Sting object exists only for one statement before it’s destroyed (a process which is called autoboxing). At this point, since we have said that it is an object, we might consider to add a property to it. So let us give it a try:
var name = "Lorem"; name.last = "Ipsum"; console.log(name.last); // undefined
The above code tries to add the property
last to the string
name. And looking at the code, it just looks fine but wait, What happened? Why didn’t it work? When we are working with regular objects, we are free to add properties at any time and they stay until you manually remove them. But with primitive wrapper types, properties seem to disappear because the object on which the property was assigned is being destroyed immediately afterward.
instanceof checks for these types of values, the result is
var name = "Lorem"; var count = 5; var found = false; console.log(name instanceof String); //false console.log(name instanceof Number); //false console.log(name instanceof Boolean); //false
instanceof operator returns
false because a temporary object is created only when a value is read. Because
instanceof doesn’t actually read anything, no temporary objects are created, and this tells us that values aren’t instances of primitive wrapper types. It has to be noted that we cannot use
Boolean objects as you would primitive values. Even though the
typeof evaluated to
Best Open Source Business Intelligence Software Helical Insight is Here
A Business Intelligence Framework
Helical IT Solution