Wrapper types in JavaScript introduction _javascript Tips

Source: Internet
Author: User

I haven't been reading Rhino books lately, the translation of the special rotten and good mouth, especially the prototype that said the mess, and then introduced by colleagues, bought this JS Advanced program design, and then continue to look at the bitter, do not spit groove, continue to say JS in a fresh sense of the packaging type.

One: String

When it comes to string types, that's interesting, we usually define a string type, as shown in the following figure:

But there is something very special in JS, that is, the string type is a basic type, not a reference type, which means that the value of string is stored on the stack, and many languages do not, such as C #, I think JS is not as a reference type is understandable, after all, it can not play multithreading, and C #中一个线程栈空间只分配1M, if a string in C # is a value type, there is the possibility of a burst stack, and JS does not have stack space constraints, so there is no explosion stack situation.

So the next question is, we often do a series of operations on string, such as substring. The following figure:

That was also said, the value of string is directly stored on the stack, then how can it have substring? According to the official website, this is the case: the string type is used to wrap s as a reference type. You then use the inner implementation of string type, which defines the substring method inside the string, so the above code should be implemented within JS.

var s=new String ("Hello")
var r=s.substring (3)
s= "Hello"

As you can see, the wrapper type is just a moment of code execution, wrapping s as a string reference type, then calling the substring method below the string reference type, and then 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 an attribute, such as color, then you read color again, you will not read the color value, such as the following figure:

If you understand the above I say principle, then you to Console.log (S.color) equals undefined is not enough to think odd, we can see, when I use s.color= "red", the JS engine found has the invocation attribute the writing, is immediately wrapped in the background to a string type, and then a new property color=red is added below the string, and then the interior immediately reset the value of s to "Hello" (s= "Hello"), Next when you console.log to output S.color, the JS engine is judged to have the invocation attribute written again, again new string ("Hello") under, naturally under this new string type does not have the color attribute, therefore returns the undefined.

Just now I also said, this packaging operation is JS in the background dynamic append and delete, the basic type into the reference type, then how much difference between the two?

<1>: This needless to say, a stack, a heap, if you know more about C #, can be considered a box and unbox operation.

<2>: We know all reference types are inherited from object, note that reference type, do not be confused by object-oriented, for example, in C #, all types are object subclass, in JS

This is not the case, we can use instanceof to see.

Two: Boolean

If you understand string this wrapper class, then in fact the Boolean wrapper class and it is a principle, but in the use of Boolean type, there is a notice, we know a reference type, unless it is null or undefined, otherwise it will always be true , and this Boolean type is exactly what this box operation is, as the following figure:

We see this time B is not a simple basic type, but a reference type, then again "and or" not the result I want. There is also a number of packaging class, this also has nothing to pay attention to, do not say.

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.