Wrapper objects


Objects are the most important data type of the JavaScript language. The values ​​of the three primitive types—numerical values, strings, and Boolean values—will also be automatically converted to objects under certain conditions, which are primitive types of "wrapper objects" (wrapper objects). ).

The so-called "wrapped objects" refer to the three primitive objects Number, String, and Boolean corresponding to numeric values, strings, and Boolean values, respectively. These three primitive objects can turn (wrap) primitive values ​​into objects.

var v1 = new Number(123);
var v2 = new String("abc");
var v3 = new Boolean(true);

typeof v1; // "object"
typeof v2; // "object"
typeof v3; // "object"

v1 === 123; // false
v2 === "abc"; // false
v3 === true; // false

In the above code, based on the value of the original type, three corresponding packaging objects are generated. It can be seen that v1, v2, and v3 are all objects, and they are not equal to the corresponding simple type values.

The design purpose of packaging objects is to make the type of "object" cover all the values ​​of JavaScript. The whole language has a common data model, and the second is to make the values ​​of primitive types have a way to call their own methods.

The three primitive objects Number, String and Boolean, if they are not called as constructors (that is, without new when called), they are called as ordinary functions and are often used to convert any type of value. Is numeric, string, and boolean.

// convert a string to a numeric value
Number("123"); // 123

// Convert the value to a string
String(123); // "123"

// convert the value to a boolean
Boolean(123); // true

For the conversion of the above data types, see the section "Data Type Conversion" for details.

To sum up, when these three objects are used as constructors (with new), they can convert values ​​of primitive types into objects; when used as ordinary functions (without new), they can be used as any type of value , Converted to the value of the original type.

Example method

Each of the three packaging objects provides many example methods, as detailed in the following text. Here are two methods they share and inherit from the Object object: valueOf() and toString().


The valueOf() method returns the value of the original type corresponding to the wrapped object instance.

new Number(123).valueOf(); // 123
new String("abc").valueOf(); // "abc"
new Boolean(true).valueOf(); // true


The toString() method returns the corresponding string form.

new Number(123).toString(); // "123"
new String("abc").toString(); // "abc"
new Boolean(true).toString(); // "true"

Automatic conversion of primitive types and instance objects

In some cases, the value of the primitive type will be automatically called as the wrapped object, that is, the attributes and methods of the wrapped object are called. At this time, the JavaScript engine will automatically convert the value of the original type into an instance of the packaged object, and destroy the instance immediately after use.

For example, a string can call the length property to return the length of the string.

"abc".length; // 3

In the above code, abc is a string, not an object itself, and the length property cannot be called. The JavaScript engine automatically turns it into a wrapper object, and calls the length property on this object. After the call is over, this temporary object will be destroyed. This is called automatic conversion between primitive types and instance objects.

var str = "abc";
str.length; // 3

// Equivalent to
var strObj = new String(str);
// String {
// 0: "a", 1: "b", 2: "c", length: 3, [[PrimitiveValue]]: "abc"
strObj.length; // 3

In the above code, the wrapper object of the string abc provides multiple attributes, and length is just one of them.

The packaging object generated by the automatic conversion is read-only and cannot be modified. Therefore, the string cannot add new attributes.

var s = "Hello World";
sx = 123;
sx; // undefined

The above code adds an attribute x to the string s, the result is invalid and always returns undefined.

On the other hand, after the end of the call, the wrapped object instance will be automatically destroyed. This means that the next time you call the properties of the string, you will actually call a newly generated object instead of the object generated during the last call, so you cannot get the properties assigned to the previous object. If you want to add attributes to a string, you can only define it on its prototype object String.prototype (see the "Object-Oriented Programming" chapter).

Custom method

In addition to native instance methods, wrapper objects can also customize methods and properties for direct call to primitive type values.

For example, we can add a double method to double strings and numbers.

String.prototype.double = function () {
  return this.valueOf() + this.valueOf();

// abcabc

Number.prototype.double = function () {
  return this.valueOf() + this.valueOf();

(123).double(); // 246

The above code is on the prototypes of the two objects String and Number, each has a custom method, which can be called on all instance objects. Note that parentheses must be added to 123 in the last line, otherwise the dot operator (.) will be interpreted as a decimal point.