"JavaScript advanced Programming" Reading notes (iii) reference types in ECMAScript _javascript tips

Source: Internet
Author: User
2.8 Reference Types

1. Object class
All classes in the ECMAScript are inherited by the object class.

The object class has the following properties:

Constructor: Reference (pointer) to the function that created the object, for the object class, which points to the original object () function

Prototype: A reference to the object's prototype of the object.

The object class also has several methods:

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

isPrototypeOf (object): Determines whether the object is a prototype of another object.

propertyIsEnumerable: Determines whether a given property can be enumerated with a for...in statement

ToString (): Returns the original string representation of an object.

valueof (): Returns the original value that best fits the object. For many classes, the method returns the same value as the return value of ToString ().

Each of the properties and methods listed above will be overwritten by other classes.

2. Boolean class
Boolean objects are rarely used in ECMAScript because they are difficult to understand, such as:
Copy Code code as follows:

var ofalseobject = new Boolean (false);
var bresult= ofalseobject && true; Outpus true

In this code, you create a Boolean object with the value of false, and then you use this value with the original value true. In a Boolean operation, the result of the and operation of false and true is false. However, in this line of code, the calculation is ofalseobject, not its value false. In a Boolean expression, all objects are automatically converted to true, so the result is true. Refer to the following code:
Copy Code code as follows:

var ofalseobject = new Boolean (false);
var bresult= ofalseobject && true; Outpus true
var bresult2= ofalseobject.valueof () && true; Outpus false

3, Number category
The ToString () method of number is described in detail in the previous article.

Number has several special methods for handling numeric values:

toFixed (parameter): Returns a string representation of a number with a specified number of decimal digits. Parameter range is 0-20

Toexponential (parameter): Returns the string form of a number represented by scientific notation. Similar to the tofixed () method, Toexponential () also has a number of decimal digits to output for a parameter. Parameter range is 0-20

Toprecision (parameter): Returns the predetermined form or exponential form of a number according to the most meaningful form. It has an argument that is used to represent the total number of numbers (excluding indices). The minimum parameter is 1

All three of these methods are rounded. Sample code:
Copy Code code as follows:

var onumber=new number (99);
Console.log (onumber.tofixed (0)); Outpus 99
Console.log (onumber.tofixed (2)); Outpus 99.00

var onumber1=new number (99);
Console.log (onumber1.toexponential (0)); Outpus 1e+2 Rounding Operation
Console.log (onumber1.toexponential (1)); Outpus 9.9e+1
Console.log (Onumber1.toexponential (2)); Outpus 9.90e+1

var onumber3=new number (99);
Console.log (onumber3.toprecision (0)); Outpus Error Precision 0 out of range
Console.log (Onumber3.toprecision (1)); Outpus 1e+2 Rounding Operation
Console.log (Onumber3.toprecision (2)); Outpus 99
Console.log (Onumber3.toprecision (3)); Outpus 99.0

4. String Class
Both the valueof () method and the ToString () method of a String object return the original value of the string type:
Copy Code code as follows:

var ostringobject=new String ("Hello World");
Console.log (ostringobject.valueof () = = Ostringobject.tostring ());//outpus true

The string class has a length property, which is the number of characters in a string, and double-byte characters are counted as one character.

The string class has a number of methods, mainly described below:

CharAt (integer parameter): Returns a single character in a string. Example:
Copy Code code as follows:

var ostringobject=new String ("Hello World");
Console.log (Ostringobject.charat (0));//outpus "H"
Console.log (Ostringobject.charat (1));//outpus "E"
Console.log (Ostringobject.charat);//outpus (a empty string)

charCodeAt (integer parameter): Returns a single character code in a string. Example:
Copy Code code as follows:

var ostringobject=new String ("Hello World");
Console.log (ostringobject.charcodeat (0));//outpus "72"
Console.log (Ostringobject.charcodeat (1));//outpus "101"
Console.log (ostringobject.charcodeat);//outpus NaN

Concat (String): Connects one or more strings to the original value of a string object. Example:
Copy Code code as follows:

var ostringobject=new String ("Hello World");
var sresult=ostringobject.concat ("Concat");
Console.log (ostringobject);//outpus "String {0=" H ", 1=" E ", 2=" L ", ...}"
Console.log (Sresult);//outpus "Hello World Concat"
alert (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:
Copy Code code as follows:

var ostringobject=new String ("Hello hello");
Console.log (Ostringobject.indexof ("Lo"));//outpus 3
Console.log (Ostringobject.lastindexof ("Lo"));//outpus 9

Localecompare (String): The string is sorted, and the return value is one of the following three:

A, if the string object is in alphabetical order before the strings in the argument, it returns a negative number (usually-1, but the true return value is determined by the specific implementation)

B, if the string object equals the strings in the argument, returns 0

C, if the string object is in alphabetical order in the argument, returns a positive number (usually 1, but the true return value is determined by the specific implementation)

Example:
Copy Code code as follows:

var ostringobject=new String ("Hello");
Console.log (Ostringobject.localecompare ("Aello")); Outpus 1
Console.log (Ostringobject.localecompare ("Hello")); Outpus 0
Console.log (Ostringobject.localecompare ("Zello")); Outpus-1

Slice (integer parameter [, integer parameter]), substring (integer parameter [, integer parameter]): Creates a string value from a substring. The first argument is the starting position of the substring to get, the second parameter is the position before the substring to get terminated, and if the second argument is omitted, the Terminator bit defaults to the string length. Neither method changes the string object's own value. Two method usages and return values are the same when the parameter is positive, only when the parameter has a negative value. For negative parameters, the slice () method uses the length of the string and substring () to treat it as 0. In addition, if there are two parameters, the second is null than the first hour, and the value returned by slice () is empty, substring () takes the smaller as the first argument. As an example:
Copy Code code as follows:

var ostringobject=new String ("Hello World");
Console.log (Ostringobject.slice (3)); Outpus "Lo World"
Console.log (Ostringobject.substring (3)); Outpus "Lo World"
Console.log (Ostringobject.slice (3,7)); Outpus "Lo W"
Console.log (ostringobject.substring (3,7)); Outpus "Lo W"
Console.log (Ostringobject.slice (3,0)); Outpus (an empty string)
Console.log (ostringobject.substring (3,0)); Outpus "Hel"

Console.log (Ostringobject.slice (-3)); Outpus "Rld"
Console.log (Ostringobject.substring (-3)); Outpus "Hello World"
Console.log (Ostringobject.slice (3,-4)); Outpus "Lo W"
Console.log (ostringobject.substring (3,-4)); Outpus "Hel"

toLowerCase (), toLocaleLowerCase (), toUpperCase (), toLocaleUpperCase (): The first two are used to convert the string to full lowercase, and the latter two to convert the string to all uppercase. toLowerCase () and toUpperCase () are the original methods, toLocaleLowerCase () and toLocaleUpperCase () are implemented based on specific areas. Example:
Copy Code code as follows:

var ostringobject=new String ("Hello World");
Console.log (Ostringobject.tolowercase ()); Outpus "Hello World"
Console.log (Ostringobject.tolocalelowercase ()); Outpus "Hello World"
Console.log (Ostringobject.touppercase ()); Outpus "HELLO World"
Console.log (Ostringobject.tolocaleuppercase ()); Outpus "HELLO World"

  
5, instanceof operator
Storing a value using a reference type when using the TypeOf operator can cause an issue that returns "Object" regardless of what type of object is referenced. ECMAScript introduced another operator, instanceof, to solve the problem.
The instanceof operator is similar to the typeof operator and is used to identify the type of object being processed. Unlike the typeof approach, the Instanceof method requires the developer to explicitly confirm that the object is a specific type. Example:
Copy Code code as follows:

var ostringobject=new String ("Hello World");
Alert (ostringobject instanceof String);//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.