In JavaScript, primitive types such as numbers, strings, booleans, nulls, and undefined are not objects themselves. However, to facilitate developers to access properties and methods, JavaScript provides a mechanism that when we try to access properties or methods of basic types, it will automatically create a corresponding one.Packaging object(wrapper object). This wrapper object is temporary and will be destroyed once the property or method is accessed.
Here are a few key understandings about JS wrapping objects:
-
Automatic boxing and unboxing:The JavaScript engine automatically performs packing and unboxing operations in the background.
Boxing:When accessing properties or methods of basic types, JavaScript will create a corresponding wrapper object (
Number
、String
、Boolean
). For example,'hello'.toUpperCase()
The string 'hello' will be boxed intoString
Object, then calltoUpperCase()
Method, finally return the result.Unboxing:The packaging object created by boxing is temporary. Once the operation is completed, the object will be destroyed and the value will be restored to the primitive type.
-
Three types of packaging:JavaScript provides three built-in packaging types:
-
String
: Used for string primitive types. -
Number
: Used for numerical basic types. -
Boolean
: Used for boolean primitive types. -
Symbol
andBigInt
There are also corresponding packaging objects, but there are relatively few usage scenarios.
-
-
Temporary:The wrapper object only exists at the moment when the attribute or method is accessed, and is destroyed immediately afterwards. This means you can't hold references to the wrapper object for a persistent time. For example, the following code does not modify the original string:
let str = 'hello'; let strObject = new String(str); // Create String objects manually = 'bar'; // Modify the wrapper object(); // undefined,The original string is not affected
Manually create wrapping objects:Although not recommended, you can use it manually
new String()
、new Number()
andnew Boolean()
Create a wrapper object. Manually created objects do not exist briefly as autoboxing, and they will continue to consume memory unless they are garbage collected. Generally speaking, manual creation of wrapping objects should be avoided, so that the JavaScript engine can automatically handle packing and unboxing.null and undefined have no wrapper objects:Try to visit
null
orundefined
The attribute or method of will be thrownTypeError
。
Example:
let num = 10; ((2)); // "10.00", automatically box into Number object, call toFixed() methodlet str = 'hello'; (); // 5. Automatically box into String objects and access the length attributelet bool = true; (()); // "true",Automatically packed Boolean Object,Call toString() method
Summarize:
Understanding the concept of JavaScript wrapping objects is essential to writing efficient and correct code. Remember, they are temporary and are used to provide access to basic type properties and methods, and generally you should have the JavaScript engine handle boxing and unboxing automatically. Avoid manually creating wrapper objects unless you have very specific needs.