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