目录
1.属性
2.方法
一、Math对象
Math 是一个内置对象,它拥有一些数学常数属性和数学函数方法。Math 不是一个函数对象。Math 用于 Number 类型。与其他全局对象不同的是,Math 不是一个构造器。Math 的所有属性与方法都是静态的。
1.属性
1.1 Math.E 属性表示自然对数的底数(或称为基数),e,约等于 2.718。由于 E 是 Math 对象的静态属性,所以应该像这样使用:Math.E,而不是作为你创建的 Math 实例对象的属性(Math 不是构造函数)。
示例:
function getNapier() {
return Math.E
}
getNapier() // 2.718281828459045
1.2 Math.PI 表示一个圆的周长与直径的比例,约为 3.14159:
function calculateCircumference () {
return Math.PI
}
console.log(calculateCircumference()); ; // 3.141592653589793
2.方法
2.1 Math.abs() Math.abs(x) 函数返回指定数字 “x“ 的绝对值。由于 Math.abs() 是 Math 中的一个静态方法,所以只需通过 Math.abs() 调用即可。
示例:
传入一个非数字形式的字符串或者 undefined/empty 变量,将返回 NaN
。传入 null 将返回 0。
Math.abs('-1'); // 1
Math.abs(-2); // 2
Math.abs(null); // 0
Math.abs("string"); // NaN
Math.abs(); // NaN
2.2 Math.ceil() 函数返回大于或等于一个给定数字的最小整数。也就是说对给定数字向上取整。
示例:
Math.ceil(1.1) // 2
Math.ceil(3.9) // 4
Math.ceil(2) // 2
Math.ceil(-1) // -1
Math.ceil(-1.1) // -1
Math.ceil(-1.8) // -1
Math.ceil(null) // 0
Math.ceil('string') // NaN
Math.ceil() // NaN
2.3 Math.floor() 与Math.ceil() 相对立,返回小于或等于一个给定数字的最大整数。Note: 可以理解 Math.floor() 为向下取整。
示例:
Math.floor( 45.95); // 45
Math.floor( 45.05); // 45
Math.floor( 4 ); // 4
Math.floor(-45.05); // -46
Math.floor(-45.95); // -46
2.4 Math.max() 函数返回一组数中的最大值。如果给定的参数中至少有一个参数无法被转换成数字,则会返回 NaN。如果没有参数,则结果为- Infinity。
示例:
Math.max(10, 20); // 20
Math.max(-10, -20); // -10
Math.max(-10, 20); // 20
2.5 Math.min() 返回零个或更多个数值的最小值。
示例:
Math.min(10, 20); // 10
Math.min(-10, -20); // -20
Math.min(-10, 20); // -10
2.6 Math.pow() 函数返回基数(base
)的指数(exponent
)次幂,即 baseexponent
。
示例:
console.log(Math.pow(7, 3));
// expected output: 343
console.log(Math.pow(4, 0.5));
// expected output: 2
console.log(Math.pow(7, -2));
// expected output: 0.02040816326530612
// (1/49)
console.log(Math.pow(-7, 0.5));
// expected output: NaN
2.7 Math.random() 函数返回一个浮点数, 伪随机数在范围从0到小于1,也就是说,从0(包括0)往上,但是不包括1(排除1),然后您可以缩放到所需的范围。实现将初始种子选择到随机数生成算法;它不能被用户选择或重置。Math.random()
不能提供像密码一样安全的随机数字。
示例:
function getRandom() {
return Math.random(); // 0~1 之间随机数
}
2.8 Math.round() 函数返回一个数字四舍五入后最接近的整数。如果参数的小数部分大于0.5,则舍入到相邻的绝对值最大的整数。如果参数的小数部分小于0.5,则舍入到响铃的绝对值更小的整数。如果参数部分恰好等于0.5,则舍入到相邻的正无穷方向上的整数。注意,Math.round()并不总是舍入到远离0的方向(尤其是在负数的小数部分恰好等于0.5的情况下。)
示例:
x = Math.round(20.49) // 20
x = Math.round(20.5) // 21
x = Math.round(-20.5) // -20
x = Math.round(-20.51) // -21
二、Date对象
创建一个 JavaScript Date
实例,该实例呈现时间中的某个时刻。Date
对象则基于 Unix Time Stamp,即自1970年1月1日(UTC)起经过的毫秒数。创建一个新Date
对象的唯一方法是通过new
操作符,例如:let now = new Date()
year
表示年份的整数值。 0到99会被映射至1900年至1999年,其它值代表实际年份。参见 示例。
monthIndex
表示月份的整数值,从 0(1月)到 11(12月)。
date可选
表示一个月中的第几天的整数值,从1开始。默认值为1。
hours
可选表示一天中的小时数的整数值 (24小时制)。默认值为0(午夜)。
minutes
可选表示一个完整时间(如 01:10:00)中的分钟部分的整数值。默认值为0。
seconds
可选表示一个完整时间(如 01:10:00)中的秒部分的整数值。默认值为0。
milliseconds
可选表示一个完整时间的毫秒部分的整数值。默认值为0。
1.常用方法
1.1Date.prototype.getDate() 根据本地时间,返回一个指定的日期对象为一个月中的哪一日(从1--31)。
示例:
获取指定日期
const birthday = new Date('August 19, 1975 23:15:30');
const date1 = birthday.getDate();
console.log(date1);
// expected output: 19
获取当前日期
let getToday = new Date()
console.log(getToday.getDate()); // 22
1.2 Date.prototype.getDay() 方法根据本地时间,返回一个具体日期中一周的第几天,根据本地时间,返回一个0到6之间的整数值,代表星期几: 0 代表星期日, 1 代表星期一,2 代表星期二, 依次类推。
示例
获取当前星期
let arr = ['星期日','星期一','星期二','星期三','星期四','星期五','星期六'] // 因为0表示星期日,所以需要放在数组首位
let now = new Date()
let now1 = now.getDay()
console.log(arr[now1]); // '星期日'
1.3 Date.prototype.getFullYear() 方法根据本地时间返回指定日期的年份。
示例:
let today = new Date();
console.log(today.getFullYear()); // 2021
1.4 Date.prototype.getHours() 方法根据本地时间,返回一个指定的日期对象的小时。返回值为一个0 到 23之间的整数值。
示例:
let now = new Date();
console.log(now.getHours()); // 14
1.5 Date.prototype.getMonth() 根据本地时间,返回一个指定的日期对象的月份,为基于0的值(0表示一年中的第一月)。因此在获取当前月份结果后应+1
onst now = new Date();
console.log(now.getMonth()+1); // 8
1.6 Date.prototype.getSeconds() 方法根据本地时间,返回一个指定的日期对象的秒数。返回值为一个 0 到 59 的整数值。
const now = new Date();
console.log(now.getSeconds()); // 21
1.7 Date.prototype.getTime() 方法的返回值一个数值,表示从1970年1月1日0时0分0秒(UTC,即协调世界时)距离该日期对象所代表时间的毫秒数。
const now = new Date();
console.log(now.getTime()); // 1629614686166
1.8 Date.now() 方法返回自 1970 年 1 月 1 日 00:00:00 (UTC) 到当前时间的毫秒数。因为 now()
是 Date
的一个静态函数,所以必须以 Date.now()
的形式来使用。注意:为了提供针对定时攻击和指纹追踪的保护,Date.now()
的精度可能会根据浏览器的高级设置项目而被取整。
console.log(Date.now()); //1629614995699
三、Array对象
JavaScript的 Array
对象是用于构造数组的全局对象,数组是类似于列表的高阶对象。
1.push:添加元素到数组的末尾 改变原数组 (打印函数体,将展示length)
let fruits = ["Apple", "Banana"]
console.log(fruits.push('Orange')) // 3
console.log(fruits) // ["Apple", "Banana","Orange"]
2.pop:删除数组末尾的元素 改变原数组 (打印函数体,将展示被删除的元素)
let fruits = ["Apple", "Banana","Orange"]
console.log(fruits.pop()) // "Orange"
console.log(fruits) // ["Apple", "Banana"]
3.shift:删除数组最前面(头部)的元素 改变原数组 (打印函数体,将展示被删除的元素)
let fruits = ["Apple", "Banana","Orange"]
console.log(fruits.shift()) // "Apple"
console.log(fruits) // ["Banana","Orange"]
4.unshift:添加元素到数组的头部 改变原数组 (打印函数体,将展示length)
let fruits = ["Banana","Orange"]
console.log(fruits.unshift("Apple")) // 3
console.log(fruits) // ["Apple","Banana","Orange"]
5.indexOf找出某个元素在数组中的索引
fruits.push('Mango');
// ["Strawberry", "Banana", "Mango"]
var pos = fruits.indexOf('Banana');
// 1
6.fill给数组填充元素 fill(value,start,end) 注意:end为开区间,不包括end 改变原数组
arr = [1,2,3,4]
console.log(arr.fill('我',1,3)); // [0,'我','我',4]
7.判断数组中是否包含某元素 返回布尔值
const array1 = [1, 2, 3];
console.log(array1.includes(2));
// expected output: true
const pets = ['cat', 'dog', 'bat'];
console.log(pets.includes('cat'));
// expected output: true
8.isArray() 判断是否为数组 返回布尔值
Array.isArray([1, 2, 3]);
// true
Array.isArray({foo: 123});
// false
Array.isArray("foobar");
// false
Array.isArray(undefined);
// false
9.slice 截取数组中的元素 slice(start,end) 不改变原数组
const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
console.log(animals.slice(2));
// expected output: Array ["camel", "duck", "elephant"]
console.log(animals.slice(2, 4));
// expected output: Array ["camel", "duck"]
10.删除或替换现有元素或者原地添加新的元素 splice(start,删除个数,需要添加的元素) 改变原数组
const months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb');
console.log(months);
// expected output: Array ["Jan", "Feb", "March", "April", "June"]
四、String对象
String
全局对象是一个用于字符串或一个字符序列的构造函数。
1.charAt() 方法从一个字符串中返回指定的字符。
let str = '12345'
console.log(str.charAt(1)) ; // 2
console.log(str.charAt(4)); // 5
2.concat() 方法将一个或多个字符串与原字符串连接合并,形成一个新的字符串并返回。 concat() 方法不改变原字符串
注意:在性能方面 强烈建议使用赋值操作符(+, +=)代替 concat 方法。
let hello = 'Hello, '
console.log(hello.concat('Kevin', '. Have a nice day.'))
// Hello, Kevin. Have a nice day.
3.includes() 方法用于判断一个字符串是否包含在另一个字符串中,根据情况返回 true 或 false。
includes() 方法是区分大小写的。例如,下面的表达式会返回 false :
'Blue Whale'.includes('blue'); // returns false
var str = 'To be, or not to be, that is the question.';
console.log(str.includes('To be')); // true
console.log(str.includes('question')); // true
console.log(str.includes('nonexistent')); // false
console.log(str.includes('To be', 1)); // false
console.log(str.includes('TO BE')); // false
4.indexOf() 方法返回调用它的 String 对象中第一次出现的指定值的索引
注意:空格也算一个下标
var anyString = "Brave new world";
console.log(anyString.indexOf('n')); // 6
5.lastIndexOf() 方法返回调用String 对象的指定值最后一次出现的索引
var anyString = "Brave new world";
console.log(anyString.lastIndexOf('r')); // 12 (第一次为1,最后一次出现为12)
6.replace() 方法返回一个由替换值(replacement)替换部分或所有的模式(pattern)匹配项后的新字符串。 该方法不改变原字符串
var anyString = "Brave new world";
console.log(anyString.replace('new','a')); //'Brave a world'
console.log(anyString); // "Brave new world"
7.replaceAll() 方法返回一个新字符串,新字符串所有满足 pattern 的部分都已被replacement 替换。 该方法不改变原字符串
var anyString = "01010101010101";
console.log(anyString.replaceAll('1','0')); //'0000000000000'
console.log(anyString); // "01010101010101"
8.split() 方法使用指定的分隔符字符串将一个String对象分割成子字符串数组,以一个指定的分割字串来决定每个拆分的位置。
let str = "Hello World. How are you doing?"
console.log(str.split(' ')); ['Hellow','World.','How','are','you','doing?']
遇到空格就分开
9.trim() 方法会从一个字符串的两端删除空白字符。trim() 方法返回一个从两头去掉空白字符的字符串,不改变原字符串
var orig = ' foo ';
console.log(orig.trim()); // 'foo'
// 另一个 .trim() 例子,只从一边删除
var orig = 'foo ';
console.log(orig.trim()); // 'foo'
10.substring() 方法返回一个字符串在开始索引到结束索引之间的一个子集, 或从开始索引直到字符串的末尾的一个子集。 该方法不改变原字符串
let str = 'hellow world'
console.log(str.substring(3,6)) // 'low'
11.toUpperCase() 方法将调用该方法的字符串转为大写形式并返回(如果调用该方法的值不是字符串类型会被强制转换)。 该方法不改变原字符串
let str = 'hellow world'
console.log(str.toUpperCase()) // 'HELLOW WORLD'
console.log(str); // 'hellow world'
12.toLowerCase() 会将调用该方法的字符串值转为小写形式,并返回。
let str = 'HELLOW WORLD'
console.log(str.toLowerCase()) // 'HELLOW WORLD'
console.log(str); // 'hellow world'