An overview of
- We can use primitive type values (such as strings)
"Hello"
) to access properties & call methods, but - Primitive type values do not support property and method Settings. The reason I can do this,
- It is because the engine has been packed and unpacked internally.
The body of the
Primitive types and complex classes
There are two types of values in JavaScript: primitives and complex values (objects).
A primitive type stores a simple value; Objects are accessed by reference. We find the actual location of object data stored in memory by the address recorded in the reference and operate on it. This is where the two categories differ substantially.
Calling methods and returning properties are ways of accessing data that are unique to objects.
let num = new Number(1234.236)
// Access properties
typeof num.toLocaleString // "function"
// Call the method
num.toLocaleString() / / "1234236"
Copy the code
But you’ll find that it’s also OK to go directly to 1234.236.
Isn’t it strange that primitive class values also have properties and methods? Otherwise, the JS engine actually does the operation behind the scenes: boxing and unboxing.
Packing and unpacking
Boxing is to convert a primitive type value into the corresponding wrapper object, for example:
let num = 1234.236
// Wrap the value into a Number object
new Number(num)
let str = 'Hello'
// A String is wrapped as a String
new String(str)
Copy the code
Num becomes new Number(num), STR becomes new String(STR), etc. Of course, this process is invisible to us. It is also because of this transformation inside JS that we can “look like we can access properties and call methods directly from primitive values”.
Of course, after the use of packing, there is a process of unpacking. Unboxing is the process of converting a wrapped object into its original value representation.
// Unpack the new Number into 1234.236
new Number(num).valueOf() / / 1234.236
// Unpack the new String into Hello
new String(num).valueOf()
Copy the code
That is, the following code is executed:
Something like the following is done:
// Box: convert to the corresponding packing object
num = new Number(1234.236)
/ / use
typeof num.toLocaleString // "function"
num.toLocaleString() / / "1234236"
/ / split open a case
num = num.valueOf()
Copy the code
That’s why there are “primitive value methods” (in quotes to indicate that they’re fake, just an appearance).
Why do you do that?
The reason for doing so must be that the advantages of this treatment outweigh the disadvantages, which can be summarized as follows:
- Convenient. There are many scenarios that operate on basic class values. It would be too much trouble to wrap a property every time you want to use it or call a method.
- In the province. You know that storing the same data (e.g
new Number(1)
和1
), objects have a higher memory overhead than values of primitive types. With the unboxing operation, we only temporarily wrap the object access when we use it, the rest of the time is still in the form of basic type values, which can save a lot of memory.
Besides, we don’t have to worry about the cost of this approach. Because JS supports this unboxing feature from birth and has internal optimizations for this type of operation, the performance issues are almost negligible.
Null /undefined has no corresponding wrapper function
JS currently has seven basic types of values: string, numeric, bigint, Boolean, Symbol, NULL, and undefined.
With the exception of null and undefined, all primitive types need corresponding wrapper functions. Therefore, accessing attributes on null and undefined is an error.
alert(null.test); // error
Copy the code
This is also understandable; undefined means the variable is not initialized, and null means the variable is empty. Neither has any real data significance, so there is no corresponding wrapper function.
Refer to the link
(End of text)
Advertising time (long term)
I have a good friend who owns a cattery, and I’m here to promote it for her. Now the cattery is full of Muppets. If you are also a cat owner and have the need, scan her “Free fish” QR code. It doesn’t matter if you don’t buy it. You can just look at it.
(after)