The JavaScript authoritative guide reading notes 2

Source: Internet
Author: User

3.6-3.8

These three chapters mainly introduce JS wrapper object, immutable primitive value and variable object reference, JS type conversion.

The wrapper object mainly refers to when the original value needs to call some methods (the original value itself is not through ".") To invoke a method or property value), JS implicitly converts the original value into a temporary object, through this temporary object to implement the original value call method (This method is the original value of the wrapper object of the method), this temporary object becomes the wrapper object. For example

Define a string and get the length of the string through the. Length () Method:

1 var a = "Hello"; 2 var b=a.length ();

A as a string variable, not an object, so he is unable to pass "." To call the length () method in the form of the But in practice, we all use this. This is because JS uses the wrapper object in this process. When string a passes "." When a property is called (the method is a property of a property value), JS creates a new temporary object by calling new String (a), that is, wrapping the object, wrapping the object to inherit all the methods of the string, and passing this wrapper object to. Length () to get the length, and return to the variable B, At the end of the process, B is assigned, the wrapper object is destroyed, and a does not change.

The wrapper object is similar to the boxed Intiger object in Java for Int.

In addition to strings string (), numbers and Booleans also have their own number () and Boolean () constructor methods to invoke. Of course, JS in addition to the implicit use of the wrapper object in the operation, you can also through the constructor to explicitly create the wrapper object, such as:

var a = ' hello '; var New String (a);

The JavaScript authoritative guide reading notes 2

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.