写在开头
ES6
常用但被忽略的方法 系列文章,整理作者认为一些日常开发可能会用到的一些方法、使用技巧和一些应用场景,细节深入请查看相关内容连接,欢迎补充交流。
相关文章
解构赋值
剔除对象不需要的属性的值
const obj = {
name: 'detanx',
age: 24,
height: 180
}
const { height, ...otherObj } = obj;
复制代码
剔除数组不需要的项
const arr = ['detanx', 24, 180]
// 剔除 第一项
const [ name, ...otherArr ] = arr
// otherArr => [24, 180]
复制代码
设置默认值
ES6
内部使用严格相等运算符(===
),判断一个位置是否有值。所以,只有当一个数组成员严格等于undefined
,默认值才会生效。(面试可能会遇到)
const [x = 1] = [undefined]
// x 1
const [x = 1] = [null]
// x null
复制代码
- 可以引用解构赋值的其他变量,但该变量必须已经声明。
let [x = 1, y = x] = []
let [x = 1, y = x] = [2]
let [x = 1, y = x] = [1, 2]
let [x = y, y = 1] = []
复制代码
不需要额外变量就可交换两个变量的值
let x = 1, y = 2
[x, y] = [y, x]
// x: 2,y: 1
复制代码
获取指定的值
function example() {
return [1, 2, 3];
}
let [a, b, c] = example();
function example() {
return {
foo: 1,
bar: 2
};
}
let { foo, bar } = example();
import { clone } from '_lodash';
复制代码
字符串
includes
- 之前判断目标字符或字符串片段是否存在某个字符串中需要使用
indexOf
来判断,返回的结果是首次出现目标的位置,不存在返回-1
。实际场景我们并不需要回去目标在字符串中的位置,只需要判断目标是否该字符串中即可,所以我们可以使用includes
。同时可以接收第二个参数,表示从左到右第几个位置开始
'detanx'.includes('tan')
'detanx'.includes('tan',3)
复制代码
查询目标字符在字符串开始或结尾
str.startsWith(char[, pos])
查询char
是否是字符串开头,pos
代表从左到右哪一个位置算起始位置。
'detanx'.startsWith('tan', 2)
复制代码
str.endsWith(char[, len])
查询char
是否是字符串,len
代表从左到右包含多少个字符(即长度)。
'detanx'.endsWith('tan', 5)
复制代码
字符串重复多少次repeat
- 重复字符串多少次,接收一个参数
- 当参数为大于
-1
并且小于1
或者为NaN
时作为0
- 大于
1
且为小数时向下取整
- 为
infinity
或者小于-1
时会报错
- 其他类型会转为数字
'detanx'.repeat(0) // ""
'detanx'.repeat(-0.7) // ""
'detanx'.repeat(NaN) // ""
'detanx'.repeat(2) // "detanxdetanx"
'detanx'.repeat({}) // ""
'detanx'.repeat([]) // ""
'detanx'.repeat(()=>{}) // ""
复制代码
字符串补全
- 从左边补全
padStart
,当字符串长度不够需要指定以什么开头,不传第二参数默认使用空格。
const x = 7
if(x < 10) String(x).padStart(2,'0')
'04'.padStart(10, 'YYYY-MM-DD');
复制代码
- 从右边补全
padEnd
,当字符串长度不够需要指定以什么结尾,不传第二参数默认使用空格。
'de'.padEnd(6, 'tanx');
复制代码
去掉头或尾空格
- 除了空格键,这两个方法对字符串头部(或尾部)的
tab
键、换行符等不可见的空白符号也有效。
- 浏览器还部署了额外的两个方法,
trimLeft()
是trimStart()
的别名,trimRight()
是trimEnd()
的别名。
// 去掉头部空格
' detanx '.trimStart(); // 'detanx '
' detanx '.trimLeft(); // 'detanx '
// 去掉尾部空格
' detanx '.trimEnd(); // ' detanx'
' detanx '.trimRight(); // ' detanx'
复制代码
数值扩展
isFinite
- 用来检查一个数值是否为有限的(
finite
),即不是Infinity
或者-Infinity
。其他类型直接返回false
,包括NaN
。
Number.isFinite(15);
Number.isFinite(0.8);
Number.isFinite(NaN);
Number.isFinite(Infinity);
Number.isFinite(-Infinity);
Number.isFinite('foo');
Number.isFinite('15');
Number.isFinite(true);
复制代码
isInteger
- 判断一个数值是否为整数。如果对数据精度的要求较高,不建议使用
Number.isInteger()
判断一个数值是否为整数。
JavaScript
采用 IEEE 754
标准,数值存储为64
位双精度格式,数值精度最多可以达到 53
个二进制位(1
个隐藏位与 52
个有效位)。如果数值的精度超过这个限度,第54位及后面的位就会被丢弃,这种情况下,Number.isInteger
可能会误判。
Number.isInteger(3.0000000000000002)
复制代码
- 这个小数的精度达到了小数点后
16
个十进制位,转成二进制位超过了53
个二进制位,导致最后的那个2
被丢弃。
- 如果一个数值的绝对值小于
Number.MIN_VALUE(5E-324)
,即小于 JavaScript
能够分辨的最小值,会被自动转为 0
,Number.isInteger
也会误判。
Number.isInteger(5E-324)
Number.isInteger(5E-325)
复制代码
isSafeInteger
Number.isSafeInteger()
是用来判断一个整数是否落在Number.MAX_SAFE_INTEGER
和Number.MIN_SAFE_INTEGER
这两个常量范围之内。
Number.isSafeInteger('a')
Number.isSafeInteger(null)
Number.isSafeInteger(NaN)
Number.isSafeInteger(Infinity)
Number.isSafeInteger(-Infinity)
Number.isSafeInteger(3)
Number.isSafeInteger(1.2)
Number.isSafeInteger(9007199254740990)
Number.isSafeInteger(9007199254740992)
Number.isSafeInteger(Number.MIN_SAFE_INTEGER - 1)
Number.isSafeInteger(Number.MIN_SAFE_INTEGER)
Number.isSafeInteger(Number.MAX_SAFE_INTEGER)
Number.isSafeInteger(Number.MAX_SAFE_INTEGER + 1)
复制代码
- 实际使用这个函数时,需要注意。验证运算结果是否落在安全整数的范围内,不要只验证运算结果,而要同时验证参与运算的每个值。
Math.trunc
Math.trunc
方法用于去除一个数的小数部分,返回整数部分。非数值,Math.trunc
内部使用Number
方法将其先转为数值。空值和无法截取整数的值,返回NaN
。
Math.trunc(4.9)
Math.trunc(-4.1)
Math.trunc(-0.1234)
Math.trunc('123.456')
Math.trunc(true)
Math.trunc(false)
Math.trunc(null)
Math.trunc(NaN);
Math.trunc('foo');
Math.trunc();
Math.trunc(undefined)
复制代码
Math.trunc = Math.trunc || function(x) {
return x < 0 ? Math.ceil(x) : Math.floor(x);
};
复制代码
Math.sign
Math.sign
方法用来判断一个数到底是正数、负数、还是零。对于非数值,会先将其转换为数值。
- 参数为正数,返回
+1
;
- 参数为负数,返回
-1
;
- 参数为
0
,返回 0
;
- 参数为
-0
,返回 -0
;
- 其他值,返回NaN。
Math.sign(-5)
Math.sign(5)
Math.sign(0)
Math.sign(-0)
Math.sign(NaN)
复制代码
- 如果参数是非数值,会自动转为数值。对于那些无法转为数值的值,会返回
NaN
。
Math.sign('')
Math.sign(true)
Math.sign(false)
Math.sign(null)
Math.sign('9')
Math.sign('foo')
Math.sign()
Math.sign(undefined)
复制代码
Math.sign = Math.sign || function(x) {
x = +x
if (x === 0 || isNaN(x)) {
return x
}
return x > 0 ? 1 : -1
}
复制代码
Math.cbrt
Math.cbrt()
方法用于计算一个数的立方根。
Math.cbrt(-1)
Math.cbrt(0)
Math.cbrt(1)
Math.cbrt(2)
复制代码
- 对于非数值,
Math.cbrt()
方法内部也是先使用Number()
方法将其转为数值。
Math.cbrt('8')
Math.cbrt('hello')
复制代码
Math.cbrt = Math.cbrt || function(x) {
var y = Math.pow(Math.abs(x), 1/3);
return x < 0 ? -y : y;
};
复制代码
Math.hypot
Math.hypot
方法返回所有参数的平方和的平方根。
Math.hypot(3, 4);
Math.hypot(3, 4, 5);
Math.hypot();
Math.hypot(NaN);
Math.hypot(3, 4, 'foo');
Math.hypot(3, 4, '5');
Math.hypot(-3);
复制代码
- 上面代码中,
3
的平方加上 4
的平方,等于 5
的平方。
- 如果参数不是数值,
Math.hypot
方法会将其转为数值。只要有一个参数无法转为数值,就会返回 NaN
。