JS Basic Learning-js Packaging objects

Source: Internet
Author: User

1     var test = "Test"; 2         TEST.A = "Hello"; 3     // undifined

Defined:
In JavaScript, "Everything is Object", arrays and functions are essentially objects, even the values of three primitive types-numeric, String, Boolean-are automatically converted to objects under certain conditions, that is, "wrapper objects" of the original type.
In layman's terms:
strings, numbers, strings, these single dogs see other people have objects, very uncomfortable, so they also made an object, called packaging.

1 console.log ("Hello World". length); 2 Console.log ("This a string". IndexOf ("a"));

In fact, when we call these methods and properties, JS Internal has implicitly helped us to create a wrapper object, the actual situation above should be this:

1 console.log (new String ("Hello World"). length); 2 console.log (new string ("This a string"). IndexOf ("a"));

The difference between wrapping an object and explicitly creating an object:

Difference 1:

The content is equal, but the storage space address is not the same

1 var a1 = "Test",2     a2=new String ("Test"); 3 console.log (a1 = = A2); // true 4 console.log (A1 = = = A2); // false

Difference 2:
Implicitly-created wrapper objects are discarded after they are used. Simply put, a big, ungrateful Han, after me, abandoned, but also to find other objects.
Take the question we start with:
TEST.A = "Hello";
This implicitly creates a wrapper object, so there is no error in assigning the value.
Console.log (TEST.A);
The previous packaging object has been discarded, but the "." is used. operator, a new wrapper object is created, but the object's property A is not assigned a value, so the value of property A is undefined.

When it comes to practical use, some browsers do not perform well, such as low-version ie, which is inefficient when dealing with strings frequently. So a lot of times, we might as well create wrapper objects directly, prevent browsers from creating too many implicit wrapper objects, and improve performance.

1 // do not recommend the use of the method 2 var example = "This is a example";   3 // It is recommended to use this method to improve performance.  4varnew String ("This is a example");

JS Basic Learning-js Wrapper object

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.