JavaScript Wrapper Object

Source: Internet
Author: User

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 properties of the string s are referenced, JavaScript converts the string value to an object by calling the new string (s), which inherits the method of the string and is used to handle the reference to the property. Once the attribute reference is finished, the newly created object is destroyed (it is not necessarily created or destroyed on the implementation, but the whole 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.

var s = "Test";                           Create a string s.len = 4;                      Set a property to it var t = S.len; Query This property

When you run this code, the value of T is undefined. The second line of code creates a temporary string object and assigns a value of 4 to its Len property, destroying the object immediately. The third line creates a new string object from the original (unmodified) string value, attempting to read its Len attribute, which naturally does not exist and the expression evaluates to undefined.

When you access a string, numeric, or Boolean property when you create a temporary object called a wrapper object, it is only occasionally used to differentiate between string values and string objects, numeric and numeric objects, and Boolean and Boolean objects. In general, the wrapper object is only seen as an implementation detail, not a particular concern. Because strings, numbers, and Boolean properties are read-only, and you cannot define new properties for them, you need to understand that they are different from objects.

It is important to note that the wrapper object can be created explicitly through the string (), number (), or Boolean () constructor:

var s = "Test", n = 1, B = true;                        A string, a number, and a 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 converts the wrapped object to its original value if necessary, so the objects S,n and B in the previous code are often--but not always--behave the same as values S,n and B. the "= =" Equals operator treats the original value as equal to its wrapper object, but the "= = =" Equality operator treats them as unequal.

JavaScript Wrapper Object

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.