How to use JavaScript packaging objects _ javascript tips-js tutorial

Source: Internet
Author: User
During javascript code execution, the basic type will find the corresponding packaging object, then the packaging object gives all the attributes and methods to the basic type, and then the packaging object will be destroyed by the system, so I understand the packaging object and can understand the reason why the Code previously written can be done. A JavaScript Object is a combination of attributes and named values. Use the "." symbol to reference attribute values. When the attribute value is a function, it is called a method.

① A piece of code that you often use but does not necessarily understand its underlying principles:

var s = "hello world!";var word = s.substring(s.indexOf(" ")+1,s.length); 

As mentioned above, the variable s is just a string of the original type. How does it have attributes (s. length) and methods (s. indexOf (), s. substring? Yes, this is related to the packaging object we are about to introduce. The reason is: as long as the attribute of String s is referenced, JavaScript will convert the String value to an object by calling new String (s). This object inherits the String (String) object method, and is used to process attribute references. Once the attribute reference ends, the newly created object will be destroyed.

Like a string, numbers and Boolean values have their own methods: create a temporary object through the Number () and Boolean () constructors. The temporary object created when you access the attributes of a string, number, or Boolean value is the encapsulated object. The remaining two null and undefined of the five original types have no encapsulated objects: accessing their properties will cause a type error (Uncaught TypeError ). After understanding the above Code, let's look at the following code:

Var s = "test"; s. len = 4; // set a property var t = s. len;

If you are not sure, the final t is 4. Isn't t equal to 4? Yes. The final t value is undefined. If you want to know why, please continue with the parsing: The second line of code here only creates a temporary String object and assigns a value of 4 to the len attribute, and then destroys this object. The third line creates a new String object through the original string s (this is not the object created by the second line of code, and the object created by the second line of code has been destroyed) and try to read its len SN. This attribute does not exist, so the result of the expression is undefined. This Code shows that reading the attribute values or methods of strings, numbers, and boolean values (in fact, they correspond to the attribute values or methods of the encapsulated object) is like an object. However, if you try to assign a value to an attribute, you will ignore this operation: the modification only happens to the temporary object, and the temporary object will not be retained.

Note: You can use the String (), Number (), Boolean () constructor to display the created packaging object:

Var s = "test", n = 1, B = true; // a String, number, and Boolean value var S = new String (s ); // A String object var N = new Number (n); // a numeric object var B = new Boolean (B); // a Boolean object

JavaScript will convert the wrapped object to the original starting value when necessary. Therefore, objects S, N, and B in the code above often do not always show the same value s, n, and B. The "=" equals operator treats the original value and its encapsulated object as equal, but the "=" equals operation treats them as unequal. The typeof operator also shows the differences between the original value and its encapsulated object:

    ①typeof(s);  ->"string"     typeof(S);  ->"object"    ②typeof(n);  ->"string"     typeof(N);  ->"object"    ③typeof(b);  ->"string"     typeof(B);  ->"object"

The above is all the content of this article. I hope you will like it.

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.