Wrapper objects in JavaScript introduction to _javascript Tips

Source: Internet
Author: User
Tags wrapper

A JavaScript object is a composite value that is a collection of attributes or named values, by means of the symbol "." To refer to the attribute value, when the property value is a function, we call it the method. We see that strings also have properties and methods:

Copy Code code as follows:

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

Since the string is not an object, why does it have attributes, as long as the attribute of the string s is referenced, JavaScript converts the string value to an object by invoking the constructor of the new string (s), which inherits the method of the string and handles the reference to the property, Once the property reference is finished, the newly created object is destroyed (in fact, the object is not necessarily created on the implementation, but the whole process looks like this).

As with strings, numbers and Boolean values also have their own methods: a temporary object is created by using the number () and the Boolean () constructor, and the invocation of these methods comes from this temporary object. This temporary object is called a wrapper object.

Attention:

Copy Code code as follows:

var s= "Test"; Declaring a string
s.len=4; Set a Len attribute to it
var T=s.len; Query This property

This time we output t should be undefined, the second line of code creates a temporary string object, assigns its Len property to 4, and then destroys the object, and the third line of code sets a new property through the original string value s, trying to read its Len property, which naturally does not exist. So the t output value is undefined.

This code shows that when reading a number, a string, a Boolean value (or method) of a property, it behaves like an object, but when trying to assign a value to its property, the action is ignored: The modification only occurs on the temporary object, and the temporary object is not persisted.

A temporary object that is created when accessing properties of a string, numeric, or Boolean value is called a wrapper object and is occasionally used to differentiate between string values and string objects, numeric and numeric objects, Boolean values, and Boolean objects

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.