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
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
primitive value wrapper type
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()
let stringValue = "hello world";
console.log(stringValue.slice(3)); // "lo world"
console.log(stringValue.slice(3, 7)); // "lo w"
- substr()
let stringValue = "hello world";
console.log(stringValue.substr(3)); // "lo world"
console.log(stringValue.substr(3, 7)); // "lo worl"
- substring()
let stringValue = "hello world";
console.log(stringValue.substring(3)); // "lo world"
console.log(stringValue.substring(3,7)); // "lo w"
- String position method
let stringValue = "hello world";
console.log(stringValue.indexOf("o")); // 4
console.log(stringValue.lastIndexOf("o")); // 7
let stringValue = "hello world";
console.log(stringValue.indexOf("o", 6)); // 7
console.log(stringValue.lastIndexOf("o", 6)); // 4
- string contains method
let message = "foobarbaz";
console.log(message.includes("bar")); // true
console.log(message.includes("qux")); // false
- trim() method
let stringValue = " hello world ";
let trimmedStringValue = stringValue.trim();
console.log(stringValue); // " hello world "
console.log(trimmedStringValue); // "hello world"
- string case conversion
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
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
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