JavaScript Wrapper Object

Source: Internet
Author: User

A JavaScript object is a composite value that is a collection of properties or named values, through the symbol "." To refer to a property value, which we call a method when the value of the property is a function. We see that the string also has properties and methods:

var s= "hello,world! ";

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

Since the string is not an object and why it has properties, 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 uses it to handle the reference to the property, as long as the property of the string s is referenced. Once the property reference is finished, the newly created object is destroyed (the implementation does not necessarily create the object, but the entire process looks like this).

As with strings, numbers and Booleans have their own methods: a temporary object is created with the number () and Boolean () constructors, and the calls to these methods are from this temporary object. This temporary object is called a wrapper object.

Note:

var s= "test"; Declares a string

s.len=4; Set a Len property for it

var T=s.len; Query This property

At this time we should output t should be undefined, the second line of code to create a temporary string object, and give its Len property is assigned a value of 4, then destroy the object, the third line of code through the original string value s set a new property, try to read its Len attribute, this property does not exist naturally, So the t output value is undefined.

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

A temporary object created when accessing a string, numeric, or Boolean property is called a wrapper object, and it is only occasionally used to differentiate between string values and string objects, numeric and numeric objects, Boolean values, and Boolean objects

JavaScript Wrapper Object

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.