JavaScript wrapper object using detailed _javascript tips

Source: Internet
Author: User
Tags numeric wrapper

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

① a piece of code that you often use but may not understand its true underlying principle:

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

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

As with strings, numbers and Boolean values have their own methods: a temporary object is created by the number () and the Boolean () constructor. A temporary object that is created when accessing properties of a string, numeric, or Boolean value is the wrapper object. The remaining two types of null and undefined in 5 original types do not have wrapper objects: accessing their properties can result in a type error (uncaught typeerror). Understand the above code, then look at the following code:

var s = "Test";
S.len = 4;//Sets a property to it
var t = S.len;

No serious students 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 look at the resolution: the original here the second line of code just creates a temporary string object and assigns the Len property to 4, and then destroys the object. The third line also creates a new string object from the original string s (this is not the object created by the second line of code, the object created by the second line of code has been destroyed) and attempts to read its Len, which naturally does not exist, so the result of the expression is undefined. This code shows that the property values or methods that read the string, numeric, and Boolean values (actually their corresponding property values or methods of the wrapped object) behave like objects. But if you try to assign a value to a property, the action is ignored: The modification only occurs on the temporary object, and the temporary object is not persisted.

Note: You can display the Create wrapper object by using string (), number (), Boolean () constructor:

var s = "Test", n=1,b=true;//a string, numeric and 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 a wrapper object to its original value if necessary, so the objects s, N, and B in the previous code often but not always behave like the values S, N, and B. the "= =" Equals operator treats the original value as equal to its wrapper object, but the "= =" congruent operation treats them as unequal. You can also see the difference between the original value and the wrapper object by using the TypeOf operator:

①typeof (s);  -> "string"
typeof (S);  -> "Object"
②typeof (n);  -> "string"
typeof (N);  -> "Object"
③typeof (b);  -> "string"
typeof (B); -> "Object"

The above is the entire contents of this article, I hope you can enjoy.

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.