Reading Notes for JavaScript advanced programming (III): reference types in ecmascript

Source: Internet
Author: User

2.8 reference type

1. Object Class

All classes in ecmascript are inherited by the object class.

The object class has the following attributes:

Constructor: A Reference (pointer) to the function that creates an object. For an object class, this pointer points to the original object () function.

Prototype: Reference to the object prototype of the object.

There are several methods for the object class:

Hasownproperty (property): determines whether an object has a specific property. The attribute value must be specified with a string.

Isprototypeof (object): determines whether the object is a prototype of another object.

Propertyisenumerable (property): determines whether a given property can be enumerated using the for... in statement.

Tostring (): returns the original string representation of the object.

Valueof (): returns the original value that best fits the object. For many classes, the value returned by this method is the same as the return value of tostring.

All the attributes and methods listed above will be overwritten by other classes.

2. boolean class

Boolean objects are rarely used in ecmascript because they are hard to understand, such:

1 VaROfalseobject =NewBoolean (False);
2 VaRBresult = ofalseobject &&True;//Outpus true

In this sectionCodeCreate a Boolean object with the value of false, and then use the value and the original value of true to perform the and operation. In the Boolean operation, the result of the "and" operation on "false" and "true" is false. However, in this line of code, ofalseobject is calculated, rather than its value false. In a Boolean expression, all objects are automatically converted to true, so the result is true. Refer to the following code:

1 VaROfalseobject =NewBoolean (False);
2 VaRBresult = ofalseobject &&True;//Outpus true
3 VaRBresult2 = ofalseobject. valueof ()&&True;//Outpus false
3. Number

The tostring () method of number is described in the previous section.ArticleIn detail.

Number has several special methods to process values:

Tofixed (parameter): returns a string representation of digits with the specified decimal digits. The parameter range is 0-20.

Toexponential (parameter): returns a string of numbers expressed in scientific notation. Similar to the tofixed () method, toexponential () also has a number of decimal places to be output. The parameter range is 0-20.

Toprecision (parameter): returns the predefined or exponential form of A number based on the most meaningful form. It has a parameter that indicates the total number (excluding the exponent ). The minimum parameter value is 1.

The preceding three methods are used for rounding. Sample Code:

 1   VaR Onumber = New Number (99 );
2 Console. Log (onumber. tofixed (0 )); // Outpus 99
3 Console. Log (onumber. tofixed (2 )); // Outpus 1, 99.00
4
5 VaR Onumber1 = New Number (99 );
6 Console. Log (onumber1.toexponential (0 )); // Outpus 1E + 2 performs rounding.
7 Console. Log (onumber1.toexponential (1 )); // Outpus 9.9e + 1
8 Console. Log (onumber1.toexponential (2 )); // Outpus 9.90e + 1
9
10 VaR Onumber3 = New Number (99 );
11 Console. Log (onumber3.toprecision (0 )); // Outpus error precision 0 out of range
12 Console. Log (onumber3.toprecision (1 ));// Outpus 1E + 2 performs rounding.
13 Console. Log (onumber3.toprecision (2 )); // Outpus 99
14 Console. Log (onumber3.toprecision (3 )); // Outpus 1, 99.0
4. string type

Both the valueof () method and tostring () method of the string object return the original value of the string type:

1 VaROstringobject =NewString ("Hello World ");
2Console. Log (ostringobject. valueof () = ostringobject. tostring ());//Outpus true

The string class has the Length attribute. It is the number of characters in a string. double-byte characters are also considered as one character.

The string class has many methods, which are described as follows:

Charat (integer parameter): returns a single character in the string. Example:

1 VaROstringobject =NewString ("Hello World ");
2Console. Log (ostringobject. charat (0 ));//Outpus "H"
3Console. Log (ostringobject. charat (1 ));//Outpus "e"
4Console. Log (ostringobject. charat (11 ));//Outpus (an empty string)

Charcodeat (integer parameter): returns the code of a single character in a string. Example:

1 VaROstringobject =NewString ("Hello World ");
2Console. Log (ostringobject. charcodeat (0 ));//Outpus "72"
3Console. Log (ostringobject. charcodeat (1 ));//Outpus "101"
4Console. Log (ostringobject. charcodeat (11 ));//Outpus Nan

Concat (string): connects one or more strings to the original value of the string object. Example:

1 VaROstringobject =NewString ("Hello World ");
2 VaRSresult = ostringobject. Concat ("Concat ");
3Console. Log (ostringobject );//Outpus "string {0 =" H ", 1 =" E ", 2 =" L ",...}"
4Console. Log (sresult );//Outpus "Hello World Concat"
5Alert (ostringobject );//Outpus "Hello World"

Indexof (string): returns the position of the specified string in another string (retrieved from the beginning of the string ).

Lastindexof (string): returns the position of the specified string in another string (retrieved from the end of the string ). Example:

 
1 VaROstringobject =NewString ("hello ");
2Console. Log (ostringobject. indexof ("Lo "));//Outpus 3
3Console. Log (ostringobject. lastindexof ("Lo "));//Outpus 9

Localecompare (string): sorts strings. the return value is one of the following:

A. If the string object is placed before the string in the parameter in alphabetical order, a negative number is returned (usually-1, but the actual return value is determined by the specific implementation)

B. If the string object is equal to the string in the parameter, 0 is returned.

C. If the string object is placed after the string in the parameter in alphabetical order, return a positive number (usually 1, but the actual return value is determined by the specific implementation)

Example:

  1   var  ostringobject =  New  string (" hello "); 
2 console. log (ostringobject. localecompare ("aello"); /// outpus 1
3 console. log (ostringobject. localecompare ("hello"); /// outpus 0
4 console. log (ostringobject. localecompare ("zello"); /// outpus-1

Slice (integer parameter [, integer parameter]) and substring (integer parameter [, integer parameter]): Create a string value from a substring. The first parameter is the starting position of the substring to be obtained, and the second parameter is the position before the substring to be obtained is terminated. If the second parameter is omitted, the ending position is the string length by default. Both methods do not change the value of the string object. When the parameter is positive, the usage and return values of the two methods are the same, and they are different only when the parameter has a negative value. For negative parameters, the slice () method adds a parameter to the length of the string, and substring () treats it as 0. In addition, if there are two parameters, the value returned by slice () is null for the second hour, and substring () takes the smaller value as the first parameter. For example:

 1   VaR Ostringobject = New String ("Hello World ");
2 Console. Log (ostringobject. Slice (3 )); // Outpus "lo world"
3 Console. Log (ostringobject. substring (3 )); // Outpus "lo world"
4 Console. Log (ostringobject. Slice (3, 7 )); // Outpus "lo w"
5 Console. Log (ostringobject. substring (3, 7 )); // Outpus "lo w"
6 Console. Log (ostringobject. Slice (3, 0 )); // Outpus (an empty string)
7 Console. Log (ostringobject. substring (3,0 )); // Outpus "El"
8
9 Console. Log (ostringobject. Slice (-3 )); // Outpus "rld"
10 Console. Log (ostringobject. substring (-3 )); // Outpus "Hello World"
11 Console. Log (ostringobject. Slice (3,-4 )); // Outpus "lo w"
12 Console. Log (ostringobject. substring (3,-4 )); // Outpus "El"

Tolowercase (), tolocalelowercase (), touppercase (), and tolocaleuppercase (): the first two are used to convert strings into lowercase letters, and the last two are used to convert strings into uppercase letters. Tolowercase () and touppercase () are original methods. tolocalelowercase () and tolocaleuppercase () are implemented based on specific regions. Example:

 1   VaR Ostringobject = New String ("Hello World ");
2 Console. Log (ostringobject. tolowercase ()); // Outpus "Hello World"
3 Console. Log (ostringobject. tolocalelowercase ()); // Outpus "Hello World"
4 Console. Log (ostringobject. touppercase ()); // Outpus "Hello World"
5 Console. Log (ostringobject. tolocaleuppercase ()); // Outpus "Hello World"
5. instanceof Operator When using the typeof operator to store values of the reference type, a problem occurs. No matter what type of object is referenced, it returns "object ". Ecmascript introduces another operator instanceof to solve this problem. The instanceof operator is similar to the typeof operator and is used to identify the type of the object being processed. Unlike the typeof method, the instanceof method requires developers to explicitly confirm that the object is of a specific type. Example:
 
1 VaROstringobject =NewString ("Hello World ");
2Alert (ostringobjectInstanceofString );//Outpus "true"
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.