Several types of packaging in JavaScript

Source: Internet
Author: User

Several types of packaging in JavaScript

One: String

String type is a basic type, not a reference type, that means that the value of string is stored on the "stack", and many languages are not like this, such as C #, I think JS is not as a reference type is excusable, after all, it can not play multi-threading, and C # in a line stacks space only allocated 1M, If string in C # is a value type, then there is the possibility of explosion, and JS has no stack space limit, so there is no explosion stack situation.

So the next question, we often do a series of operations on string, such as substring.

That said just now, the value of string is stored directly on the stack, then how can it have substring? As explained by Codego.net, this is done by wrapping s into a reference type using the string type. Then use the internal implementation of the string type, exactly the string internal definition of the substring method, so in fact, the above code in the internal JS should be implemented in this way.

123 vars=new String("hello")varr=s.substring(3)s="hello"


As you can see, the wrapper type is just executing the code for an instant, wrapping s into a string reference type, then invoking the substring method under the string reference type, and then re-assigning the "Hello" value to S, and the final effect is s= "Hello", r= "Lo" , if you look closely, you will find that if I give s dynamic attached to a property, such as color, then you read the color again, is not to read the color value of the use of s.color= "red" when the JS engine found that the invocation of the properties of the wording, Will immediately wrap it in the background as a string type, then give the string a new property color=red, and the inside will immediately reset the value of s to "Hello" (s= "Hello"), Next when you console.log to output S.color, JS engine to determine the invocation of the properties of the writing, again new string ("Hello"), naturally under the new string type is not the Color property, so returned undefined.

A stack, a heap, if you are familiar with C #, can be thought of as a box and unbox operation. All reference types are inherited from object, note that is a reference type, not to be confused by object-oriented, such as in C #, all types are object subclasses, in JS is not so, we can use instanceof to see.

Two: Boolean

If you understand the wrapper class of string, then actually the Boolean wrapper class is a principle, but in the use of the Boolean type, there is a note, we know a reference type, unless it is null or undefined, otherwise it is always true , and this Boolean type is exactly the box operation we see at this time that B is not a simple basic type, but a reference type, and then it is no longer "with or" the result I want.


This article is from the "Xibalangei" blog, make sure to keep this source http://9897987.blog.51cto.com/9887987/1628518

Several types of packaging in JavaScript

Related Article

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.