Redo JS Virtuoso Sixth bullet: basic type packaging

Source: Internet
Author: User

The type in JS is already known to have Boolean,string,number,undefined,function,object, where boolean,number,string is a value type. The so-called basic type wrapper is to wrap the value type in the base type as a reference type with the

The corresponding is boolean,number,string, whose main function is to increase the corresponding operation method for the value type, because the value type itself has no method, the method is the object type.

1 Packaging principle

Temporary encapsulation, such as Var str= ' hello '; var str2=str.substring (2): Llo; The basic process is var str=new String (' hello '); str.substring (2); str=null; After the temporary use of the recovery, this process is given to the JS engine to complete.

So the basic type encapsulation is simple to understand to provide extension methods for value types, and the basic package type does not have to or rarely see the display build object. such as: Var sr=new String (' Hello World ');

2 Boolean=>boolean

Basically no expansion

3 Number=>number

1) toFixed: Keep several decimals, JS reserved decimal will be automatically rounded, such as: Var a=10.059;a.tofixed (2), the result is 10.06;

2) Toexponential: Using exponential representation of numbers, such as Var num=10;num.toexponential (1), structure 1.0e1, parameters are reserved for the number of decimal numbers;

3) Toprecision: This method is compatible with the above 2 methods, which means that the number of valid digits is reserved, and its parameters represent the number of effective bits;

such as Var num=99,

Num.toprecision (1); 1e2;

Num.toprecision (2); 99;

Num.toprecision (3); 99.0;

3 string=>string

This is the most complex and basic package type available.

1)concat: Multiple strings concatenated, but the new string returned, here is the same method as the array, but in most cases the use of the + sign is very high;

2)Chartat(); Gets the character at the location of the specified parameter, such as Var str= ' Hello World '; Str.chartat (2); The result is: L;chartcodeat, the same as before except that the ASC encoding of the character is returned ;

fromchartcode: convert ASC encoding to character;

3)indexOf and lastIndexOf: Find the location of the target string, different search direction, return to the target location subscript;

4)trim: Remove the space before and after the character;

5)Touppercase,tolocaleuppercase,tolowercase,tolocalelowercase: no explanation;

6)match(pattern): match here is the same as regular expression

7)Search(pattern): Returns the first index of the regular expression that satisfies;

8)Replace: Receive 2 parameters, the first is the replaced string, if you do not use a regular expression, replace only the first matching regular expression, to replace all strings will require a regular expression, the second argument is a new string

9)split: Receives 2 parameters, the purpose is to divide the string into an array;

Localecompare: Indicates that the parameter string is compared to the string itself, if the first letter of the argument string is sorted alphabetically before the first character of the string itself, returns 1, equals 0, and Vice-1

11) The most complex 3 methods

Slice: Receives 2 parameters, representing the substring that obtains the starting and ending position;

subString: Receives 2 parameters, which means to get the starting and ending position substring, and the same as slice does not contain the character of the cutoff position;

subStr: Accepts 2 parameters, indicates the Get start position, the second argument is the string length;

Redo JS Virtuoso Sixth bullet: basic type packaging

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.