Special type of reference

Source: Internet
Author: User

Today we have learned three special reference types: Boolean,number and string. I also reviewed the knowledge that I learned before.

First, we know that the base type value is not an object, so there should be no method. However, they are actually available in a way. Whenever a primitive type value is read, the background creates an object of the corresponding wrapper type, which allows us to invoke some methods to manipulate the data.

Calling typeof on an instance of the base wrapper type returns "Object", and all objects of the base wrapper type are converted to Boolean true.

(i) Boolean type

To create a Boolean object, you can call the Boolean constructor and pass in true or false.

var boolean1=new Boolean (True);

An instance of the Boolean type overrides the ValueOf () method, returns the base type value of TRUE or FALSE, overrides the ToString () method, and returns the string "true" or "false".

Currently, the most common problem with Boolean types is the use of Boolean objects in Boolean expressions. For example:

var one=new Boolean (false);

var result=one&&true;

alert (result);//Output True

In a Boolean operation, False&&true equals false, but here it is true. This is because this is evaluated for one instead of its value (false), and all objects in the Boolean expression are converted to True,true&&true results of course the output is true.

var One=false;

var result=one&&true;

alert (result);//Output False

In this case, the value of one is evaluated, so the false&&true result output is false.

Also, there are two differences between the base type and the Boolean value of the reference type, and the TypeOf operator returns "Boolean" to the base type, and "object" to the reference type. Second, because the Boolean object is an instance of the Boolean type, testing the Boolean object with the instanceof operator returns True, and the Boolean value of the test base type returns FALSE.

(b) Number type

The number type also overrides the valueof (), tolocalestring (), and ToString () methods. ValueOf () returns the numeric value of the base type represented by the object. The other two returns a numeric value in the form of a string.

The method provided by the number type:

The ToFixed () method returns a string of numeric values in the specified number of decimal digits, and it can also be rounded automatically. For example:

var num=10.005;

Alert (num.tofixed (2));//Output "10.01"

The Toexponential () method returns the string form of a number in exponential notation. The parameter is also the number of decimal digits in the specified output result.

var num=10;

Alert (num.tofixed (1));//output "1.0e+1";

The Toprecision () method may return the fixed format, or it may return the exponential format, mainly to see which is appropriate. It is also the number of digits that receive a parameter that represents a numeric value.

var num=99;

Alert (num.toprecision (1));//"1e+2"

Alert (Num.toprecision (2));//99

Alert (Num.toprecison (3));//99.0

Three String type

The inherited valueof (), tolocalestring (), and ToString () methods all return the base string value represented by the object.

The string type also provides a number of methods:

The CharAt () method returns the character of the given position as a single-byte string. For example:

var one= "Blue";

Alert (One.charat (1));//Output "L"

If you want to get a character encoding, you can use charCodeAt ().

The Concat () method is used to stitch one or more strings together, returning a new string that is stitched up. The original string is not changed at the same time.

var one= "Hello"

var result=one.concat ("World", "!");

alert (result);//output "Hello world!"

Three methods for creating a new string based on substrings: Slice (), substr (), and substring (). All three methods return a substring of the manipulated character, and all receive one or two parameters. The first parameter of the slice () and substring () methods specifies where the substring starts, and the second specifies the end position. SUBSTR () The second parameter specifies the number of characters that are returned. None of the three methods will modify the original string.

var one= "Hello World";

Alert (One.slice (1));//"Ello World"

Alert (one.substring (1));//"Ello World"

Alert (ONE.SUBSTR (1));//"Ello World"

Alert (One.slice (1,7));//"Ello W"

Alert (one.substring (1,7));//"Ello W"

Alert (ONE.SUBSTR (1,7));//"Ello Wo"

When the parameter is negative, the results of these three methods are very different.

The slice () method adds an incoming negative number to the length of the string.

The substr () method adds the first argument to the length of the string, and the second parameter is 0.

The substring () method turns the negative argument to 0.

String position method: IndexOf () and LastIndexOf (). Searches for a given substring from a string, returning the position of the substring.

The trim () method creates a copy of the string, removing all whitespace from the predecessor and suffix.

toLowerCase (), toLocaleLowerCase () and toUpperCase (), tolocaleuppercase () Case Conversion method

The string type provides a method for matching patterns in a string.

The match () method receives only one parameter, a regular expression, or a RegExp object.

var text= "Cat,bat,sat,fat";

var pattern=/.at/;

var matches=text.match (pattern);

Alert (matches[0]);//"Cat"

The search () method parameter is the same as the match () method parameter. Returns the index of the first occurrence in a string.

The replace () method receives two parameters: the first argument can be a string or a RegExp object, and the second argument can be a string or a function.

If the first argument is a string, only the first substring is replaced. To replace all substrings, you need a regular expression, and you want to specify global G.

var text= "Cat,bat,sat,fat";

var result=text.replace ("At", "on");

alert (result);//Output "Con,bat,sat,fat"

Result=text.replace (/at/g, "on");

alert (result);//"Con,bon,son,fon"

The Localecompare () method is used to compare two strings, and the string returns a negative number before the alphabet, followed by a positive number, and returns 0;

var num= "Body";

Alert (Num.localecompare ("cat"));//-1

Alert (Num.localecompare ("body"));//0

Alert (Num.localecompare ("Ace"));//1

Special type of reference

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.