JavaScript Tour-Fifth stop: Talk about the types of packaging that are called

Source: Internet
Author: User

Recently did not read the Rhino book, the translation of the special rotten and a good mouthful, especially the prototype that piece said the mess, and later by colleagues introduced, bought this JS Advanced program design, and then continue

Hard to see, do not spit groove, continue to say that JS has a fresh sense of the packaging type.

One: String

When it comes to string types, it's pretty interesting, usually. Defines a string type, such as:

But in JS there is a very special point, that is, the 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

So, for example, 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 is only allocated 1M, if the string in C # is a value class

Type, then there is the possibility of explosive stack, 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. Such as:

That said just now, the value of string is stored directly on the stack, then how can it have substring? According to the official website explanation is this: this time will use the string type to wrap s

The reference 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.

1 var s=New String ("Hello")2var r=s.substring (3)3 s= " Hello

As you can see, the wrapper type is just an instant in executing the code, wrapping s into a string reference type, then invoking the substring method under the string reference type, and then re-

The "Hello" value assigned to S, 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

, the color value is not read, for example:

If you understand the above I said the principle, then you to Console.log (S.color) equals undefined is not enough to think, we can see, when I use s.color= "red",

The JS engine finds that there is a calling property that is dynamically wrapped in the background as a string type, and then adds a property color=red to the string, and then the inside immediately resets

The value of the new setting S is "Hello" (s= "Hello"), and then when you console.log to output S.color, the JS engine determines the method of invoking the property again, new String ("Hello")

Below, nature has no color property under this new string type, so it returns undefined.

Just now I also said that this packaging operation is JS in the background dynamically 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 are familiar with C #, can be thought of as a box and unbox operation.

<2>: We know that all reference types are inherited from object, note that reference types are not confused by object-oriented, such as in C #, all types are object subclasses, in JS

It is not so, we can use instanceof to look at it.

Two: Boolean

If you understand string this wrapper class, then actually the Boolean wrapper class and it is a principle, but in the use of the Boolean type, there is a note, we

Knowing a reference type, unless it is null or undefined, it is always true, and this Boolean type is exactly what this box does, such as:

We see that at this time B is not a simple basic type, but a reference type, and then it is no longer "with or" the result I want. There is also a number wrapper class, which also

There is nothing to pay attention to, not to say.

JavaScript Tour-Fifth stop: Talk about the types of packaging that are called

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.