JavaScript Wrapper Object

Source: Internet
Author: User
Tags type null

A JavaScript object is a composite value: It is a collection of properties and named values. Through the "." Symbol to reference the property value. When a property value is a function, it is called a method.

① a piece of code that you often use but don't necessarily understand the underlying principle:

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

As previously mentioned, here the variable s is just a string primitive type, how does it have attributes (S.length) and Methods (S.indexof (), s.substring ())? Yes, it's about the packaging object we're about to introduce. The reason for this is that JavaScript converts a string value to an object by invoking the new string (s) as long as the property of the string s is referenced, which inherits the method of the string object and is used to handle the reference to the property. Once the attribute reference is finished, the newly created object is destroyed.

As with strings, numbers and Booleans have their own methods: Create a temporary object with the number () and Boolean () constructors. The temporary object created when accessing a string, numeric, or Boolean property is the wrapper object. 5 remaining two types in the original type null and undefined do not wrap objects: Accessing their properties results in a type error (Uncaught TypeError). Understand the above code, then look at the following code:

var s = "Test"; S.len = 4;//Sets an attribute to it var t = S.len;

Not serious classmates here will think that the last T is equal to 4. Isn't the last t equal to 4? Yes, the value of the last T is undefined. Want to know why please continue to look at parsing: The second line of code here simply creates a temporary string object and assigns a value of 4 to the Len property, destroying the object. The third line creates a new string object from the original string s (the object that is not created by the second line of code, the object created by the second line of code has been destroyed) and tries to read its Len Zodiac, which naturally does not exist, so the result of the expression is undefined. This code illustrates the same behavior as objects that read the property values or methods of strings, numbers, and booleans (in effect, they correspond to the property values or methods of the wrapper object). However, if you attempt to assign a value to a property, the action is ignored: the modification occurs only on the temporary object, and the temporary object does not persist.

Note: The Create wrapper object can be displayed by using the string (), number (), 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 wrapped objects to original values when necessary, so objects S, N, and B in the previous code are often but not always the same as the values S, N, and B. the "= =" Equals operator treats the original value as equal to its wrapper object, but the "= = =" Equality operation treats them as unequal. You can also see the difference between the original value and its wrapper object through the TypeOf operator:

①typeof (s); "String"

typeof (S); "Object"

②typeof (n); "String"

typeof (N); "Object"

③typeof (b); "String"

typeof (B); "Object"

  

  

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.