Home >Web Front-end >JS Tutorial >'JavaScript Advanced Programming' Reading Notes (3) Reference Types in ECMAScript_Javascript Skills

'JavaScript Advanced Programming' Reading Notes (3) Reference Types in ECMAScript_Javascript Skills

WBOY
WBOYOriginal
2016-05-16 17:55:59962browse

2.8 Reference types

1. Object class
All classes in ECMAScript are inherited from the Object class.

The Object class has the following properties:

Constructor: a reference (pointer) to the function that creates the object. For the Object class, the pointer points to the original Object() function

Prototype: A reference to the object's prototype.

The Object class also has several methods:

HasOwnProperty(property): Determine whether the object has a specific property. The attribute value must be specified with a string

IsPrototypeOf(object): Determine whether the object is the prototype of another object.

PropertyIsEnumerable(property): Determine whether the given property can be enumerated using the for...in statement

ToString(): Return 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().

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

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

Copy Code The code is as follows:

var oFalseObject = new Boolean(false);
var bResult= oFalseObject && true; //outpus true

In this code, a Boolean object is created with a false value, and then the AND operation is performed with this value and the original value true. In Boolean operations, the result of the AND operation between false and true is false. However, in this line of code, oFalseObject is evaluated, not its value false. In Boolean expressions, all objects are automatically converted to true, so the result is true. Refer to the code below:
Copy code The code is as follows:

var oFalseObject = new Boolean(false );
var bResult= oFalseObject && true; //outpus true
var bResult2= oFalseObject.valueOf() && true; //outpus false

3. Number class
Number’s toString() method is introduced in detail in the previous article.

Number has several special methods for processing numerical values:

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

toExponential (parameter): Returns the string form of a number expressed in scientific notation. Similar to the toFixed() method, toExponential() also has a parameter for the number of decimal places to be output. The parameter range is 0-20

toPrecision (parameter): Returns the predetermined form or exponential form of the number according to the most meaningful form. It has one parameter, which is the total number of numbers (excluding the exponent). The minimum parameter is 1

The above three methods will perform rounding operations. Sample code:
Copy code The code is 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 has been rounded
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 has been rounded
console.log(oNumber3.toPrecision(2)); / /outpus 99
console.log(oNumber3.toPrecision(3)); //outpus 99.0

4. String class
 valueOf() of String object Both the method and the toString() method will return the original value of String type:
Copy code The code is as follows:

var oStringObject=new String("Hello world");
console.log(oStringObject.valueOf() == oStringObject.toString());//outpus true

String class It has a length attribute, which is the number of characters in the string. Double-byte characters are also counted as one character.

The String class has a large number of methods, which are mainly introduced as follows:

charAt (integer parameter): Returns a single character in the string. Example:
Copy code The code is 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(11));//outpus (an empty string)

 charCodeAt(integer parameter): return The code for a single character in a string. Example:
Copy code The code is 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(11));//outpus NaN

 concat (string): Concatenate one or more strings to the original value of the String object. Example:
Copy code The code is 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 within 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 The code is as follows:

var oStringObject=new String("Hello Hello" );
console.log(oStringObject.indexOf("lo"));//outpus 3
console.log(oStringObject.lastIndexOf("lo"));//outpus 9

LocaleCompare (string): Sorts strings. The return value is one of the following three:

A. If the String object is alphabetically sorted before the string in the parameter, 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, return 0

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

Example:
Copy code The code is 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]): 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 end of the substring to be obtained. If the second parameter is omitted, the termination position defaults to the length of the string. Neither of these methods changes the value of the String object itself. When the parameter is positive, the usage and return value of the two methods are the same, and they are different only when the parameter has negative value. For negative parameters, the slice() method will add the length of the string to the parameter, and substring() will treat it as 0. In addition, if there are two parameters, the second one is smaller than the first one, the value returned by slice() is empty, and substring() will use the smaller one as the first parameter. For example:
Copy the code The code is 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 all lowercase, and the last two are used to convert the string to all uppercase. toLowerCase() and toUpperCase() are original methods, while toLocaleLowerCase() and toLocaleUpperCase() are implemented based on specific areas. Example:
Copy code The code is 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
When using the typeof operator to store values ​​using reference types, there will be a problem. No matter what type of object is referenced, it will return "object". ECMAScript introduced another operator instanceof to solve this problem.
The instanceof operator is similar to the typeof operator and is used to identify the type of object being processed. Unlike the typeof method, the instanceof method requires the developer to explicitly confirm that the object is of a specific type. Example:
Copy code The code is as follows:

var oStringObject=new String("hello world" );
alert(oStringObject instanceof String); //outpus "true"
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn