Introduction
We all know that JavaScript data types are divided into two categories: basic types (or primitive types) and reference types.
Values of basic types are simple segments of data stored in the stack memory, and they are accessed by value. There are five basic types in JS: Undefined, Null, Boolean, Number, and String.
A reference type value is an object stored in heap memory, and its value is accessed by reference. The reference types are mainly Object, Array, Function, RegExp, and Date.
Objects have properties and methods, so it is not surprising that we see the following code.
var favs=['Egg','Lotus Pot'];favs.push('Okra');console.log(favs);//["Egg", "Lotus Pot", "Okra"]console.log(favs.length);//3Array is a reference type, so it can naturally have attributes (length) and methods (push), which is natural like having to eat ice cream in summer. But, look at the following code and think about it carefully, is this, this, this, legal?
var realMessage="Said I love you but I lied";var myMessage=realMessage.substring(5,15);console.log(myMessage); //"I love you"
There is a heartbroken girl who will do the "substring" method willfully perform the "substring" method on a string used to break up, and then happily watch the cut and fall asleep. But, but, isn't it said that string is a basic type? Why can it have methods? ? Is there any other kingly law, Master Qingtian!
In fact, all of this is because of something called "basic packaging type". This basic packaging type is particularly honest, and it is the real "returning the clothes after the matter, hiding the merits and fame"!
Basic packaging type
In addition to the reference types such as Object and Array mentioned at the beginning, JavaScript also provides us with three special reference types: String, Number and Boolean, which facilitates us to operate the corresponding basic types.
Continue to look at the above example of clipping strings. Have you noticed that despite using the substring method, the value of realMessage itself will not change. Calling this method only returns a new string.
This is what the basic packaging type does. Originally, you don’t have a method, but when you want to use it, you can just adjust it and the corresponding basic packaging type has this method. For example, the substring method above, it is impossible for the basic type of string to have this method, but the packaging type of String has it, it will whisper and return the result after executing this method. On execution to:
A lot of things happened when realMessage.substring(5,15) this line of code.
First, it will read the value of realMessage from memory. When in this reading mode, the background starts to work. JS elevation describes these actions completed in the background like this:
1. Create an instance of String type;
2. Call the specified method on the instance;
3. Destroy this instance
The above example can be explained in code like this:
var _realMessage=new String("Said I love you but I lied");var myMessage=_realMessage.substring(5,15);_realMessgae=null; //Destroy after the method is calledSo, in this way we understand that it is not that the basic type string executes its own method, but the background creates a corresponding basic wrapper type String for it. It instantiates an instance based on the value of the basic type, letting this instance call the specified method, and finally destroy itself, which is moving.
Pay attention to the "destroy" feature of the last step of the basic wrapper type, which determines that we cannot add custom properties and methods to the basic type values.
var me="sunjing";me.age=18;console.log(me.age);//undefined
I added the age attribute to the "me" string, and the value is set to be beautiful 18 years old. However, when I accessed again, this attribute was gone. This is because:
When the second line of code attribute assignment is performed, an instance of the basic wrapper type is created in the background. This age attribute is indeed hung on the instance, but immediately afterwards, the instance is destroyed. When executing to the third line, a new instance of the basic wrapper type is recreated, and naturally there is no age attribute.
Show basic packaging type
In addition to the string in read mode, the background will help us create the basic wrapper type instance, which we can also display.
var str=new String("hello");var str2=str.toUpperCase();console.log(str2);//"HELLO:This is different from what is saved in the variable when the background helps us create.
var str1=new String("hello");var str2="hello";typeof str1 //"object"typeof str2 //"string"Summarize
Thanks to the basic packaging type, it is more convenient for us to operate the three basic types of string, boolean, and number. Whenever these three basic type values are read, the background will create the corresponding wrapper type instance. This instance will call the specified method and will be destroyed after calling. This short life cycle determines that we cannot add custom properties and methods to the primitive types.
The above article has a deep understanding of why strings can be owned in JavaScript. This is all the content I share with you. I hope it can give you a reference and I hope you can support Wulin.com more.