This article mainly introduces the reasons for using the string method in JavaScript. For more information, see
Introduction
We all know that JavaScript data types are classified into two categories: Basic Types (or primitive types) and reference types.
Values of the basic type are simple data segments stored in the stack memory, which are accessed by value. Javascript has five basic types: Undefined, Null, Boolean, Number, and String.
The value of the reference type is an object stored in the heap memory, and its value is accessed by reference. The reference types include Object, Array, Function, RegExp, and Date.
The object has attributes and methods, so it is not surprising to see the following code.
Var favs = ['egg ', 'swapping']; favs. push ('okra '); console. log (favs); // ["eggs", "Lotus", "okra"] console. log (favs. length); // 3
Array is a reference type, so it can naturally have attributes (length) and methods (push), just as it must be ice cream in summer. However, let's look at the following code and think about it. Is this legal?
var realMessage="Said I love you but I lied";var myMessage=realMessage.substring(5,15);console.log(myMessage); //"I love you"
A heartbroken female arbitrarily executes the "substring" method on a string used to break up, and then happily watched the clip version go to bed. But isn't string a basic type? Why does it have a method ?? There is no Wang FA, Old Man Qingtian!
In fact, this is because there is a stuff called "basic packaging type. This basic packaging type is very straightforward. It is the real "success and name "!
Basic packaging type
In addition to the reference types of objects and arrays mentioned at the beginning, JavaScript also provides three special reference types: String, Number, and Boolean, so that we can easily operate on the corresponding basic types.
Looking at the clip string example above, have you noticed that although the substring method is used, the value of realMessage itself will not change. Calling this method only returns a new string.
This is the role of the basic packaging type. You don't have a method, but when you want to use a method, you just need to call this method for the corresponding BASIC packaging type. For example, in the above substring method, this method is not allowed for the basic type of string, but there is a packaging type of String. It will return the result after executing this method. Run the following command:
realMessage.substring(5,15)
A lot of things happen in this line of code.
First, it reads the value of realMessage from the memory. In this read mode, the background starts to work. The JS elevation describes the Operations completed in the background as follows:
1. Create an instance of the String type;
2. Call the specified method on the instance;
3. Destroy this instance
The code above can be used to describe:
Var _ realMessage = new String ("Said I love you but I lied"); var myMessage = _ realMessage. substring (5, 15); _ realMessgae = null; // The method is destroyed after it is called.
Therefore, we can understand that it is not the basic type string that executes its own method, but the background creates a corresponding BASIC packaging type String for it, it generates an instance based on the value of the basic type, so that the instance can call the specified method and destroy itself.
Note that the "destroyed" feature of the basic packaging type in the last step determines that we cannot add custom attributes and methods for basic type values.
var me="sunjing";me.age=18;console.log(me.age);//undefined
I added the age attribute to the "me" string and set it to a beautiful 18-year-old. However, when I accessed the string again, this attribute had no trace. This is because:
When you assign values to the second line of code, an instance of the basic packaging type is created in the backend. The age property is attached to the instance, but the instance is destroyed immediately. When the third row is executed, a new instance of the basic packaging type is re-created. Naturally, there is no age attribute.
Display basic packaging type
In addition to the string reading mode, the backend will help you create a basic package type instance, and we can also create it explicitly.
var str=new String("hello");var str2=str.toUpperCase();console.log(str2);//"HELLO:
This is different from what is saved in variables when the background helps us create them.
var str1=new String("hello");var str2="hello";typeof str1 //"object"typeof str2 //"string"
Summary
Thanks to the basic packaging type, it is more convenient to operate the three basic types: string, boolean, and number. Each time you read these three basic types of values, the backend will create a corresponding packaging type instance. This instance will call the specified method and the call will be destroyed. This short life cycle determines that we cannot add custom attributes and methods for basic types.
Let's take a look at the subString () method and slice () method of the String class in javascript.
I recently read the book "Javascript advanced programming" and found some useful skills and knowledge that I have never touched before. I want to record it through my blog to deepen my memory.
The subString () method and slice () method in the String class are described in section 2.8.4 of this book. The method and return result are basically the same, as shown in the following example:
Var strObj = new String ("hello world"); alert (strObj. slice (3); // output result: "ol world" alert (strObj. subString (3); // output result: "ol world" alert (strObj. slice (3, 7); // output result: "lo w" alert (strObj. subString (3, 7); // output result: "lo w"
From the output results of the above code, we can see that the method called by the slice () method and subString () method are exactly the same as the output result, the two methods return the substrings of the strings to be processed. Each method accepts one or two parameters. The first parameter is the starting position of the substring to be obtained, the second parameter is to obtain the end position of the substring. If the second parameter is omitted, the default end position is the length of the String, and neither method changes the value of the String object.
Why are there two methods with identical functions? In fact, these two methods are not exactly the same, but they process parameters slightly differently only when the parameters are negative.
For negative parameters, the slice () method adds a parameter to the length of the string, and the subString () method treats it as 0, for example:
Var strObj = new String ("hello world"); alert (strObj. slice (-3); // output result: "rld" alert (strObj. subString (-3); // output result: "hello world" alert (strObj. slice (3,-4); // output: "lo w" alert (strObj. subString (3,-4) // The output result is as follows"
In this way, we can see the main differences between slice () and subString () methods. If the parameter is only-3, slice () returns "rld", and subString () returns "hello world ". This is because for the string "hello world", slice (-3) will be converted to slice (8), and subString (-3) will be converted to subString (0 ). Similarly, the difference between 3 and-4 is obvious. The slice () method is converted to slice (), which is the same as the preceding example and returns "lo w ". The subString () method interprets the two parameters as subString (), which is actually subString (), because subString () always uses smaller parameters as the start bit, A large number is the most terminating digit.