


'JavaScript Advanced Programming' Reading Notes (3) Reference Types in ECMAScript_Javascript Skills
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
var oStringObject=new String("hello world" );
alert(oStringObject instanceof String); //outpus "true"

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



When a generic function handles pointer types in Go, it will receive a reference to the original variable, allowing the variable value to be modified. Reference types are copied when passed, making the function unable to modify the original variable value. Practical examples include using generic functions to compare strings or slices of numbers.

es2017 is es8. The full name of es is "ECMAScript", which is a universal scripting language implemented according to the ECMA-262 standard. The version officially released in June 2017 is officially called ECMAScript2017 (ES2017). Because it is the 8th version of ECMAScript, it can Referred to as es8.

How to solve the C++ compilation error: 'invalidinitializationofreferenceoftype'type&'fromexpressionoftype'type''? Problem background: In C++ programming, we sometimes encounter compilation errors. One of them is the error message "invalidinitializationofreferenceof

This article will sort out and share with you the features of ECMAScript. It will take you an hour to quickly understand all the features of ES6~ES12. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

In Go language, data types can be divided into value types and reference types. Value types directly store the value of data, while reference types store the memory address of the data. In the Go language, the following data types are reference types: Slice: A slice is a dynamic array that can dynamically grow or shrink as needed. Slices are reference types and are actually references to the underlying array. By modifying the elements of a slice, you can change the value of the underlying array. Sample code: packagemainimport"

Go language is a strongly typed language, and its data types can be divided into two types: reference data types and value data types. Reference data types and value data types are slightly different in use. Let's take a closer look at these two data types. 1. Reference data types Reference data types in Go language include slices, maps, channels, interfaces and pointers. For reference data types, the value of a variable is not just its own value, but a pointer to a memory address. Therefore, when we declare a variable of reference type, a memory location is allocated for it

Overview of reference types in Go language Go language is an open source programming language developed by Google. One of its design goals is to be concise, efficient, and easy to use. In the Go language, reference types are a special data type that store references to data in memory rather than the data itself. This article will introduce reference types in Go language and provide specific code examples. Reference types include slices, maps, channels, and functions. These types are used in Go

In Go language, variable names are used directly when passing reference type parameters without pointers.
