Chapter 5 of JavaScript Advanced Programming Reading Sharing - Basic Reference Types

JavaScript Advanced Programming (4th Edition) Reading Sharing Note Recording

Applicable to comrades who are just getting started

ECMAScript variables can contain two different types of data: primitive values ​​and reference values.

A reference value (or object) is an instance of a particular reference type. In ECMAScript, reference types are
structures that group data and functionality together.

Date

The rookie tutorial can view the properties and methods of the specific Date type

To create a date object, use the new operator to call the Date constructor:
let now = new Date();
  • ECMAScript also provides the Date.now() method, which returns the number of milliseconds representing the date and time when the method was executed .
// 起始时间
let start = Date.now();
  • The valueOf() method of the Date type does not return a string at all, this method is rewritten to return the milliseconds of the date
var d=new Date();
var n=d.valueOf();
  • getTime() returns the millisecond representation of the date; same as valueOf()
var d = new Date();
var n = d.getTime();

RegExp

ECMAScript supports regular expressions through the RegExp type.

primitive value wrapper type

In order to facilitate the operation of primitive values, ECMAScript provides 3 special reference types: Boolean , Number and String .

Boolean

JavaScript Boolean Object | Novice Tutorial

Number

JavaScript Number Object | Novice Tutorial

  • The toFixed() method returns a numeric string containing the specified number of decimal places
let num = 10; 
console.log(num.toFixed(2)); // "10.00"
  • The isInteger() method and safe integers, ES6 added the Number.isInteger() method to identify whether a value is stored as an integer
console.log(Number.isInteger(1)); // true 
console.log(Number.isInteger(1.00)); // true 
console.log(Number.isInteger(1.01)); // false

String

JavaScript String Object | Novice Tutorial

  • The charAt() method returns the character at the given index, specified by the integer parameter passed to the method
let message = "abcde"; 
console.log(message.charAt(2)); // "c"
  • concat(), used to concatenate one or more strings into a new string
let stringValue = "hello "; 
let result = stringValue.concat("world"); 
console.log(result); // "hello world" 
console.log(stringValue); // "hello"
  • ECMAScript provides 3 methods for extracting substrings from strings

In any case, omitting the second argument means extracting to the end of the string

  • slice()
The first parameter indicates the position where the substring starts, and the second parameter indicates the position where the substring ends
The second parameter is the position where the extraction ends (that is, the characters before this position will be extracted, not including this position)
let stringValue = "hello world"; 
console.log(stringValue.slice(3)); // "lo world"
console.log(stringValue.slice(3, 7)); // "lo w"

  • substr()

The first parameter indicates the position where the substring starts, and the second parameter indicates the number of substrings to return.
let stringValue = "hello world";
console.log(stringValue.substr(3)); // "lo world"
console.log(stringValue.substr(3, 7)); // "lo worl"
  • substring()

The first parameter indicates the position where the substring starts , and the second parameter indicates the position where the substring ends
The second parameter is the position where the extraction ends (that is, the characters before this position will be extracted, not including this position)
let stringValue = "hello world";
console.log(stringValue.substring(3)); // "lo world"
console.log(stringValue.substring(3,7)); // "lo w"
  • String position method
indexOf() and lastIndexOf(), these two methods search the incoming string from the string and return the position ( or -1 if not found ).
let stringValue = "hello world"; 
console.log(stringValue.indexOf("o")); // 4 
console.log(stringValue.lastIndexOf("o")); // 7
Both methods can receive an optional second parameter indicating the position to start the search.
let stringValue = "hello world"; 
console.log(stringValue.indexOf("o", 6)); // 7 
console.log(stringValue.lastIndexOf("o", 6)); // 4
  • string contains method
ECMAScript 6 adds 3 methods for judging whether a string contains another string: startsWith() ,
endsWith() includes()
let message = "foobarbaz";
console.log(message.includes("bar")); // true 
console.log(message.includes("qux")); // false
  • trim() method
ECMAScript provides trim() method on all strings . This method creates a copy of the string, before deleting,
After all spaces, return the result.
let stringValue = " hello world "; 
let trimmedStringValue = stringValue.trim(); 
console.log(stringValue); // " hello world " 
console.log(trimmedStringValue); // "hello world"
  • string case conversion
toLowerCase() toLocaleLowerCase()、toUpperCase() toLocaleUpperCase()
let stringValue = "hello world"; 
console.log(stringValue.toLocaleUpperCase()); // "HELLO WORLD" 
console.log(stringValue.toUpperCase()); // "HELLO WORLD" 
console.log(stringValue.toLocaleLowerCase()); // "hello world" 
console.log(stringValue.toLowerCase()); // "hello world"

singleton built-in object

Math

ECMAScript provides the Math object as a place to save mathematical formulas, information and calculations

min() and max() methods

let max = Math.max(3, 54, 32, 16); 
console.log(max); // 54 
let min = Math.min(3, 54, 32, 16); 
console.log(min); // 3

//要知道数组中的最大值和最小值,可以像下面这样使用扩展操作符
let values = [1, 2, 3, 4, 5, 6, 7, 8]; 
let max = Math.max(...val);

rounding method

  • The Math.ceil() method always rounds up to the nearest integer.
  • The Math.floor() method always rounds down to the nearest integer.
  • The Math.round() method performs rounding.
  • The Math.fround() method returns the closest single precision
console.log(Math.ceil(25.9)); // 26 
console.log(Math.ceil(25.5)); // 26 
console.log(Math.ceil(25.1)); // 26 
console.log(Math.round(25.9)); // 26 
console.log(Math.round(25.5)); // 26 
console.log(Math.round(25.1)); // 25 
console.log(Math.fround(0.4)); // 0.4000000059604645 
console.log(Math.fround(0.5)); // 0.5 
console.log(Math.fround(25.9)); // 25.899999618530273 
console.log(Math.floor(25.9)); // 25 
console.log(Math.floor(25.5)); // 25 
console.log(Math.floor(25.1)); // 25

random() method

The Math.random() method returns a random number in the range 0 to 1 , including 0 but not 1 .
You can use Math.random() to randomly select a number from a set of integers based on the following formula:
function selectFrom(lowerValue, upperValue) { 
 let choices = upperValue - lowerValue + 1;  //通过将这两个值相减再加 1 得到可选总数
 return Math.floor(Math.random() * choices + lowerValue); 
}

Values ​​in the range a~b, inclusive of a and b

Guess you like

Origin blog.csdn.net/weixin_42307283/article/details/129161488