Secrets behind strings in JS

Source: Internet
Author: User

When I was a child, I thought that when I grew up, I could be a man and a man, but who knows that I am going astray, and now I have embarked on a road of no return. Now, I have learned how to be safe, read and Write technical blogsCodeIn this way, it's easy!

It is not easy to do technology. A small problem may be tortured for half a day, and it is not easy to solve the problem. It cannot be solved in an instant. The customer does not know how many times they have been scolded in their hearts, who can understand our hearts ......

When talking about a few pessimistic statements, we should like to continue with our grief. Once the problem is solved, the mood, the feeling, and the last words are a thousand words.

The statement about whether everything in Javascript is an object is controversial. For example, we can call the method directly on the string literal defined by single quotes or double quotes:

 
"This is liuyu". Length

 

This can obtain the correct results. We know that objects can be considered before a point, because the correct answer is obtained here, indicating that the string is indeed an object, but the problem is that a string that uses a literal instead of a constructor is not an object? I am not talking nonsense. Obviously there is an answer above. The object that can call attributes or methods is of course. Yes. What we see is true, but what is the truth?

The fact is that a string defined by literal is not an object, or it can only be said to be a pseudo object at most, because when a method or attribute is called in the string literal,ProgramIt will silently create a temporary String object that is equal to the literal value, then call the attribute or method on the temporary object, and finally return the result, this temporary String object will be destroyed after the mission is completed. There is also strong evidence that you can add an attribute on the string literal or change an attribute value, but the result cannot be successful:

 
VaR S = "this is liuyu"; S. Name = "liuyu"; alert (S. Name );

 

The reason for undefined is: S. the name attribute is added to the temporary String object secretly created by the Program (which may cause memory leakage), but we cannot get reference to the object created by this temporary character, and the temporary String object will soon be destroyed. Therefore, adding an attribute to a string literal or changing the attribute value does not make any sense, because it will not be affected at all.

There is also evidence that the typeof operator is used to tell us that the string type is "string", which is completely different from that of "object. So why can strings use object operators?

In fact, each of the three primary primitive data types (numbers, strings, and Boolean) has its own object class. Javascript not only provides numeric, string, and Boolean types, but also provides numeric, string, and Boolean classes. These classes encapsulate the corresponding raw data types. They not only have their original data values, but also define the methods for attributes and operation values. Javascript can flexibly convert various data types. When you access the properties or methods of a string, JavaScript automatically creates its packaging object. This string packaging object will replace the original string value, which has defined attributes and methods, so this method is correct.

 
VaR S = "this is liuyu"; // This is the original data value var S = new string ("this is LiYu"); // This Is A String object

 

The string is automatically converted to a string object as needed. When you use the + operator for S, a corresponding original string value will be created for computation.
Remember, all the content discussed here applies to both numeric and Boolean types. Finally, all numeric values, strings, and boolean values can be converted to their corresponding objects through the object () function: VaR number_wrapper=Object(22) Reference: http://www.ineeke.com/archives/1184/ Http://www.cnblogs.com/2050/archive/2012/08/17/2644189.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.