In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
In this article Xiaobian for you to introduce in detail the "JavaScript string object instance analysis", the content is detailed, the steps are clear, the details are handled properly, I hope this "JavaScript string object instance analysis" article can help you solve your doubts, following the editor's ideas slowly in-depth, together to learn new knowledge.
When writing js code, we often call the length property of the string, the substring () method, the charAt (), indexOf () method, and so on. It is important to note that string objects in js are also immutable. Create a string object by + concatenating strings. It is recommended to use [] .join () to concatenate strings.
Look at the following example:
Var str= "javascript"; var len=str.length;var substr=str.substring (2jol Len)
So the question is, why does a string not an object have an attribute? We know that when wrapped with a "or" sign in js, we all call it a string. When a string calls the length property or its method, JavaScript will hermit convert the value of the string into an object by calling new String (str), which inherits the method of the string and is used to handle the reference to the property. Once the reference ends, the newly created object is automatically destroyed. Like strings, numbers and Boolean types have their own methods, creating a temporary object through the new Number () and new Boolen () constructors, from which calls are made. Null and undefined do not wrap objects, and accessing their properties results in a type error.
Now let's look at an example of a simulation process:
Var str = "test"; str.length = 10; str.testStrLen = 10; var res = str.length; console.log (res); / / output 4 res = str.testStrLen; console.log (res); / / output undefined
When js runs into this code, the value of res outputs 4, undefined. The second line of code attempts to overwrite the original attribute length of the string, and the third adds an attribute testStrLen. From the result of the final output, the expected result has not been achieved. The reason is that the second and third lines each create a temporary string object, and then destroy the temporary object. When the value is obtained by lenth or the lenth attribute in the string, when you try to read the value of testStrLen and find it through the corresponding prototype chain, a undefined is returned if it is not found. By testing this code, it shows that when reading a string, the result is the same as an object call. However, if you try to assign a value to its property, the operation is ignored, and the modification occurs only on the temporary object, which is not saved.
Temporary objects created when accessing properties of string, numeric, and Boolean values are called wrapper objects and are occasionally used to distinguish between string values and string objects, numeric and numeric objects, and Boolean and Boolean objects. The wrapper object is only seen as an implementation detail without special attention. Because the properties of strings, numbers, and Boolean values are read-only, and you cannot define new properties for them, it helps to understand that they are different.
Note that the wrapper object can be displayed through the String (), Number (), and Boolean () constructors.
Var str = new String ("test"); str.length = 10; str.testStrLen = 10; var res = str.length; console.log (res); / / output 4 res = str.testStrLen; console.log (res); / / output 10
Through the above code, we successfully added the testStrLen attribute to the string object str, but did not successfully overwrite the original length attribute, so we can deduce that the prototype chain reads its own attributes first and does not look up the next step if it is found. The successful appending of testStrLen can also help us understand that a temporary string object is created when attributes are appended directly through a string.
Then let's compare the equivalence between packing and unpacking.
Var str1 = new String ("test"); var str2 = "test"; console.log (str1 = = str2); / / true console.log (str1 = str2); / / false var N1 = new Number (1); var N2 = 1; console.log (N1 = = N2); / / true console.log (N1 = N2); / / false var b1 = new Boolean (false); var b2 = false; console.log (b1 = b2); / / true console.log (b1===b2) / / false
From this we can see that JavaScript converts the wrapper object to the original value if necessary. The "=" operator treats the original value as equal to its wrapper object. But the "= =" congruence operator treats them as unequal. You can see the difference between the original value and its wrapper object through the typeof operator.
For example:
Var str1 = new String ("test"); var str2 = "test"; console.log (str1 = = str2); / / true console.log (str1 = str2); / / false console.log (typeof str1); / / object console.log (typeof str2); / / string var N1 = new Number (1); var N2 = 1; console.log (N1 = = N2); / / true console.log (N1 = = N2); / / false console.log (typeof N1) / / object console.log (typeof N2); / / number var b1 = new Boolean (false); var b2 = false; console.log (b1 = = b2); / / true console.log (b1 = b2); / / false console.log (typeof b1); / / object console.log (typeof b2); / / boolean
Common typeof return types
After reading this, the article "JavaScript string object instance Analysis" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself to understand it. If you want to know more about related articles, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.