Introduction to packaging types in Javascript _ javascript skills

Source: Internet
Author: User
This article mainly introduces the packaging types in Javascript. The packaging type refers to packaging data into reference types at the moment of code execution. This article describes this small knowledge, if you need a friend, you can refer to what I have not read recently. The translation is really bad and easy to understand, especially the prototype. Later I introduced it to my colleagues, I bought this js advanced program design, and then I continued to look at it with great pressure. I will continue to talk about the new packaging types in js.

I. String

Speaking of the String type, it is very interesting. We usually define a string type like this, for example:

However, in js, the string type is of the basic type and does not belong to the reference type. Therefore, the string value is stored on the stack, this is not the case for many languages. For example, C #, I think js is also excitable to be used as a reference type. After all, it cannot play with multithreading, while C # Only allocates 1 M of thread stack space, if string is a value type in C #, there is a possibility of stack explosion, but js has no stack space restrictions, so there is no stack explosion.

The next question is that we often perform a series of operations on the string, such as substring. For example:

As mentioned earlier, the value of string is directly stored on the stack. How can it have substring? According to the explanation on the official website, the String type will be used to package s as the reference type. Then, use the internal implementation of the String type. The substring method is defined inside the String type. Therefore, the above Code should be implemented in the internal js.

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

We can see that the packaging type only wraps s into the String reference type at the moment of code execution, and then calls the substring method under the String reference type, then, the "hello" value is assigned to s. The final effect is s = "hello", r = "lo". If you observe carefully, you will find that, if I add a dynamic attribute for s, such as color, then if you read the color again, the value of color will not be read, for example:

If you understand the principles mentioned above. log (s. color) equals to undefined, which is not surprising. We can see that when I use s. when color = "red" is used, the js engine immediately packs the calling attributes into the String type in the background, then, a new property color = red is added to the String, and the value of s is set to "hello" (s = "hello") immediately. Next, you can use the console. log to output s. when the color is used, the js engine determines that there is a way to write a call attribute. When the new String ("hello") is used again, there is no color attribute under the new String type, so undefined is returned.

As I mentioned earlier, this type of packaging operation is dynamically appended and deleted by js in the background, and the basic type is converted to the reference type. What is the difference between the two?

<1>: Needless to say, a stack and a heap can be considered as a box and unbox operation if you have a better understanding of C.

<2>: we know that all the reference types are inherited from objects. Note that they are reference types. Do not confuse them with object-oriented objects. For example, in C #, all types are subclasses of objects, in js

This is not the case. You can use instanceof to check it.

Ii. Boolean

If you understand the String packaging class, the Boolean packaging class is actually a principle, but there is a note in the use of the Boolean type. We know a reference type, unless it is null or undefined, it will always be true, and this Boolean type exactly performs this box operation, such:

We can see that at this time, B is not simply a basic type, but a reference type. At this time, "and or" will no longer produce what I want. There is also a Number packaging class, which does not have any precautions.

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.