JavaScript wrapper object Introduction and basic packaging type

Source: Internet
Author: User
Tags numeric wrapper

Objects in JavaScript are composed of two basic elements of a property and method. The former is an object that implements the loading unit of the information in the process of carrying out its required behavior, which is associated with the variable, which means that the object can be executed according to the designer's intention, which is associated with a particular function. 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;//Set 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, number, 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 basic wrapper type of JavaScript

To facilitate manipulation of basic type values, ECMAScript also provides three special reference types: Boolean, number, and string. These types are similar to other reference types and also have special behaviors corresponding to their respective base wrapper types. In fact, whenever a base type value is read, the background creates an object of the corresponding base wrapper type, allowing us to invoke methods to manipulate the data.

var S1 = "some text";
var s2 = s1.substring (2);

The variable S1 in this example contains a string, and the string is of course the base type value. The next line calls the S1 substring () method and saves the returned results in S2. We know that basic type values are not objects, so logically they should not have a method (but they do have a method). In fact, in order for us to achieve this intuitive operation, the background has automatically completed a series of processing. When the second line of code accesses S1, the access procedure is in a read mode that reads the value of the string from memory. When you access a string in read mode, the following processing is done automatically in the background:

(1) Create an instance of type string.

(2) invokes the specified method on the instance.

(3) Destroy this instance.

You can use the following code to represent:

var S1 = new String ("some text");
var s2 = s1.substring (2);
S1 = null;

After this process, the basic string value becomes the same as the object. Furthermore, the above three steps also apply to Boolean and numeric values corresponding to the Boolean and number types.

The main difference between a reference type and a basic wrapper type is the life cycle of the object. An instance of a reference type created using the new operator is kept in memory until the execution stream leaves the current scope. An object of the base wrapper type that is automatically created exists only in the execution period of this line of code (instantaneously) and then destroys immediately. This means that we cannot add properties and methods to the property at run time.

var S1 = "some text";
S1.color = "Red";
alert (S1.color); Undefined

Of course, you can display objects that invoke Boolean, number, and string to create the base wrapper type, but do not recommend doing so. Calling typeof on an instance of the base wrapper type returns "Object", and all objects of the base wrapper type are converted to a Boolean value of true.

var obj = new Object ("some text");
Alert (obj instanceof String)//true

It is worth noting that invoking the constructor of the base wrapper type using new is not the same as calling a transition function with the same name directly.

var value = "25";
var number = number (value);//Transition function
Alert (typeof number)//number

var obj = new number (VAR); Constructors
Alert (typeof obj)//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.