JS Basic Packaging type Description Requirements Document

Source: Internet
Author: User

To facilitate operation of the basic type values, ES also provides three special reference types, i.e. (Basic wrapper type): Number,string,boolean. These three types are similar to the reference types described earlier, but also have some characteristics of the basic data types.

Often see people in the code to create a string/number type, and then use the string/number type of methods, such as split (), substring (), and so on, make us mistakenly think of the basic packaging type of methods. In fact, our understanding is wrong. In fact, when we manipulate the basic wrapper type data, we silently create an object of the corresponding basic wrapper type in the background. Because it is an object, we are able to invoke the methods owned by the object.

As an example:

var str = "Hello,1024idea"; var strsub = str.substring (2,7); Console.log (strsub);//llo,1

Execute the above code, in fact, in the background will perform the following steps:

    1. Create an object instance of the basic wrapper type

    2. function that invokes an object instance

    3. Destroy this object.

As shown below:

var str = "Hello,1024idea", var strobj = new String (str), var strobjsub = strobj.substring (2,7); Strobj.null;console.log ( STROBJSUB);//llo,1

The difference between a reference type and a basic wrapper type: The life cycle of the object.

An instance of a reference type that is created with the new keyword, and the object exists in the current scope. The object that is automatically created by the basic wrapper type exists at the time the object is called and is destroyed when the call is complete.

The Object () constructor, which returns an instance of the base wrapper type, based on the type of parameter you passed in. When you pass in a string, an instance of type string is returned, an instance of type number is returned, and a Boolean instance is returned when the value is passed in. Such as:

var obj = new Object ("Hello,1024idea"); Console.log (obj instanceof String);//True

Note: Constructors created with the New keyword have a different effect than using a transform function with the same name. Such as:

The difference between the return value of the constructor and the transformation function var str = "1024x768"; var strobj = new String (str); Console.log (typeof strobj);//objectvar strnum = number (str ); Console.log (typeof strnum);//number

In this example, the variable strobj holds the object type, while the variable strnum holds the number type.

In general, it is not recommended to manually create an object of the basic wrapper type, as this often creates the problem of not being aware of whether you are dealing with a basic wrapper type or a reference type.

Boolean type

The Boolean type is a reference type created by a Boolean value. To create a Boolean type, you can use the Boolean constructor to create and pass in a true or false parameter. Such as:

var booleanobject = new Boolean (true);

Note: In Boolean expressions that use a Boolean object, we may make mistakes in some places, such as:

var booleanobj = new Boolean (false); var result = Booleanobj && true;console.log (result);

What do you think will be printed on it? Most people might say false because it's a fake, so it's false. However, the fact is that the result is true because Booleanobj is an object, and in a Boolean expression all objects are converted to true. Remember: The boolean value of a reference type is actually an object.

Of course, we do not recommend this (it is not recommended to use Boolean values of reference types to determine Boolean expressions)

Love Creation Classroom--professional front-end technical training

Annual salary 300,000 is not a dream

Number Type

The number type is a reference type created by a numeric value. To create a number type, you can pass a numeric value parameter by using a constructor of type #, such as:

var numberobj = new Number (1024);

Here is a method for a number type: numberobj.tofixed (); You can accept a numeric value that represents the number of reserved decimals (automatically rounded). In our usual projects, we often have to deal with price problems or growth rates, that is, how many decimal places to keep, which is very effective with this method.

Number type var numberobj = new Number (1024.153), Console.log (numberobj.tofixed (2));//1024.15console.log ( Numberobj.tofixed (1));//1024.2

String type

The string type is a reference type that is created by the value.

The way string is created by using the string constructor, which can pass in string parameters such as:

var stringojb = new String ("Hello,1024idea");

Each instance of the string type has a length property, which is used to evaluate how long a string is (that is, how many characters it contains).

character Method : CharAt (), charCodeAt (), takes a numeric parameter, returns the character or character encoding of a numeric index position.

The ES5 also defines another way to access a character in a string, which can be accessed in square brackets [] in a browser that supports ES5.

String type var str = "Hello,1024idea"; Console.log (Str.charat (4));//oconsole.log (Str.charcodeat (4));// 111console.log (Str[4]);//o

the method of manipulating strings:concat (), you can concatenate multiple strings together. Returns the resulting new string.

var str1 = "Hello,"; var str2 = Str1.concat ("1024idea"); Console.log (str2);//hello,1024idea

In fact, in practice, we usually use more or "+" to splice the string.

Split () splits the string with a delimiter, returning the array. You can also pass in a parameter, the "delimiter".

Slice (), substring (), substr (). These three are functions that intercept a string. where Slice () is very similar to substring (), the first parameter is the starting position of the Intercept, the second parameter is the stop position of the intercept, and the first parameter of substr () is the starting position, and the second parameter is the length to intercept.
These three functions do not change the original string, but create a copy on the original string. Returns the value after the copy of the operation.

the location of the string method:indexOf (), lastIndexOf (); Start querying substrings from the position of index 0/find a string starting at the last index. If found, returns the position of the substring, if not found, returns-1; The first argument is the substring to find, the second argument is optional, and if so, the search starts at a location.

The trim () method is also defined in ES5 to remove all whitespace at the beginning and end of the string. In addition, for some later versions of Firefox,safari,chrome, the nonstandard trimleft () and TrimRight () methods are also supported to remove the beginning or trailing spaces of a string.

Casing conversion method for strings: toLowerCase (), toUpperCase ();


JS Basic Packaging type Description Requirements Document

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.