Javascript Primitive Wrappers








Three special reference types are created for three primitive values:

  • Boolean type
  • Number type
  • String type

The three types act like the other reference types, and have a special behavior for their primitive-type equivalents.

Every time a primitive value is read, an object of the corresponding primitive wrapper type is created assist access to methods for manipulating the data.





Example

var s1 = "hello world";
var s2 = s1.substring(2);
console.log(s2);

s1 is a primitive string value. And substring() method was called on s1 and saved the return value to s2.

Primitive values aren't objects, so they shouldn't have methods. Any time a string value is accessed in read mode, the following three steps occur:

  1. Create an instance of the String type.
  2. Call the specified method on the instance.
  3. Destroy the instance.

For var s2 = s1.substring(2); its actual code is

var s1 = new String("hello world");
var s2 = s1.substring(2);
s1 = null;




Note

In this way the primitive string value acts like an object. Boolean and numeric values have the identical behaviours, respectively.

When you instantiate a reference type using the new operator, it stays in memory until it goes out of scope, whereas automatically created primitive wrapper objects exist for only one line of code.

Properties and methods cannot be added at runtime for primitive types.

var s1 = "hello world";
s1.color = "red";
console.log(s1.color);   //undefined

String object created in the second line is destroyed by the time the third line is called.

The third line creates its own String object, which doesn't have the color property.

wrapper objects

We can create the primitive wrapper objects using the Boolean, Number, and String constructors.

Calling typeof on an instance of a primitive wrapper type returns "object"

All primitive wrapper objects convert to the Boolean value true.

Object constructor

Object constructor acts as a factory method and can return an instance of a primitive wrapper based on value passed in. For example:

  • When a string is passed into the Object constructor, an instance of String is created.
  • When a number is passed into the Object constructor, an instance of number is created.
  • When a Boolean is passed into the Object constructor, an instance of Boolean is created.

var obj = new Object("hi");
console.log(obj instanceof String);   //true

The code above generates the following result.

Note 2

Calling a primitive wrapper constructor using new is not the same as calling the casting function of the same name. For example:


var value = "5";
var number = Number(value);   //casting function
console.log(typeof number);   //"number"

var obj = new Number(value);    //constructor
console.log(typeof obj);              //"object"

In this example, number is filled with a primitive number value of 5 while the variable obj is filled with an instance of Number.

The code above generates the following result.