ECMAScript 6 (ES6) 数值的扩展

又到了学习的时间了

说实话作为一个小菜鸡我只想偷偷懒,可是还是得硬着头皮写博客

我的命怎么这么苦

哈哈哈别当真,还是得好好学习天天向上。。。
在这里插入图片描述

一,数值的扩展

(1)二进制和八进制表示法

ES6 提供了二进制和八进制数值的新的写法,分别用前缀0b(或0B)和0o(或0O)表示。

// 数值转为二进制字符
(369).toString(2); // "101110001"
// 数值转为八进制字符
369..toString(8); // "561"

// 二进制用0b或oB表示
0b101110001 === 369; // true
0B101110001 === 369; // true

// 八进制用0o或0O表示
0o561 === 369; // true
0O561 === 369; // true

上面代码中,数值369没有直接调用toString()方法,如果不加括号直接调用toString(),这个点会被 JavaScript 引擎解释成小数点,从而报错。有以下方法可以避免这个问题。

  • 数值加上括号(())
  • 在数值后面加两个点,JavaScript 会把第一个点理解成小数点,把第二个点理解成调用对象属性,从而得到正确结果
  • 通过方括号运算符([])调用
// 数值加()调用
(234).toString(2); // "11101010"
// 数值加.调用
234..toString(2); // "11101010"
// 通过[]调用
234['toString'](2); // "11101010"

如果要将0b和0o前缀的字符串数值转为十进制,要使用Number方法。

Number('0b111')  // 7
Number('0o10')  // 8

(2)Number.isFinite(), Number.isNaN()

ES6 在Number对象上,提供了 Number.isFinite()Number.isNaN() 两个方法。

  • Number.isFinite() 用来检查一个数值是否为有限的;
  • Number.isNaN() 用来检查一个数值是否为NaN。
// Number.isFinite()检查一个数是否是有限的
Number.isFinite(15); // true
Number.isFinite(0.8); // true

// 参数类型不是数值,一律返回false
Number.isFinite(NaN); // false
Number.isFinite(Infinity); // false
Number.isFinite(-Infinity); // false
Number.isFinite('jidi'); // false
Number.isFinite('1'); // false
Number.isFinite(true); // false


// Number.isNaN()检查一个数是否为NaN
Number.isNaN(NaN) // true
Number.isNaN(9/NaN) // true
Number.isNaN('true' / 0) // true
Number.isNaN('true' / 'true') // true

// 参数类型不是NaN,一律返回false
Number.isNaN(15) // false
Number.isNaN('15') // false
Number.isNaN(true) // false

(3)Number.parseInt(), Number.parseFloat()

ES6 将全局方法parseInt()和parseFloat(),移植到了Number对象上面,行为完全保持不变。

// ES5写法
parseInt('123'); // 123
parseInt('   81'); // 81
parseInt(1.23); // 1
parseInt('8a'); // 8
parseInt('12**'); // 12
parseInt('12.34'); // 12
parseInt('15e2'); // 15
parseInt('15px'); // 15
parseInt('abc'); // NaN
parseInt('.3'); // NaN
parseInt(''); // NaN
parseInt('+'); // NaN
parseInt('1000', 2); // 8
parseInt('1000', 6); // 216
parseInt('1000', 8); // 512

// ES6写法
Number.parseInt('123'); // 123
Number.parseInt('   81'); // 81
Number.parseInt(1.23); // 1
Number.parseInt('8a'); // 8
Number.parseInt('12**'); // 12
Number.parseInt('12.34'); // 12
Number.parseInt('15e2'); // 15
Number.parseInt('15px'); // 15
Number.parseInt('abc'); // NaN
Number.parseInt('.3'); // NaN
Number.parseInt(''); // NaN
Number.parseInt('+'); // NaN
Number.parseInt('1000', 2); // 8
Number.parseInt('1000', 6); // 216
Number.parseInt('1000', 8); // 512

这样做的目的,是逐步减少全局性方法,使得语言逐步模块化。

Number.parseInt === parseInt // true
Number.parseFloat === parseFloat // true

(4)Number.isInteger()

Number.isInteger()用来判断一个数值是否为整数。

Number.isInteger(25) // true
Number.isInteger(25.1) // false

//JavaScript 内部,整数和浮点数采用的是同样的储存方法,所以 25 和 25.0 被视为同一个值。
Number.isInteger(25) // true
Number.isInteger(25.0) // true

//如果参数不是数值,Number.isInteger返回false。
Number.isInteger() // false
Number.isInteger(null) // false
Number.isInteger('15') // false
Number.isInteger(true) // false

根据国际标准 IEEE 754,JavaScript 提供的有效数字最长为53个二进制位。当数值的精度超过这个标准,Number.isInteger()会失真。

// 超过精度范围,判断失真
Number.isInteger(1.000000000000000000000000000000000000002); // true

(5)Number.EPSILON

ES6 新增一个极小的常量Number.EPSILON,它表示 1 与大于1的最小浮点数之间的差。由于数字在javaScript内部使用64位浮点数存储,所以Number.EPSILON相当于2的-52次方。

Number.EPSILON === Math.pow(2, -52); // true
Number.EPSILON; // 2.220446049250313e-16

由于浮点数的计算是不精确的,Number.EPSILON的实质是一个可以接受的最小误差范围。

// 浮点数的计算是不精确的
0.1 + 0.7; // 0.7999999999999999

// 在最小误差范围内,可以认为两个数相等
2 + Number.EPSILON === 2;

(6)安全整数和 Number.isSafeInteger()

  1. JavaScript 能够准确表示的整数范围在-253和253之间(开区间),超过这个范围,无法精确表示这个值。
  2. ES6引入了Number.MAX_SAFE_INTEGER和Number.MIN_SAFE_INTEGER这两个常量,用来表示这个范围的上下限。
  3. Number.isSafeInteger()则是用来判断一个整数是否落在这个范围之内.。
Math.pow(2, 53) - 1; // 9007199254740991
Number.MAX_SAFE_INTEGER === Math.pow(2, 53) - 1; // true
Number.MAX_SAFE_INTEGER === 9007199254740991; // true

Number.MIN_SAFE_INTEGER === -Number.MAX_SAFE_INTEGER; // true
Number.MIN_SAFE_INTEGER === -9007199254740991; // true

// 判断一个数是否在-9007199254740991~9007199254740991之间
Number.isSafeInteger('jidi'); // false
Number.isSafeInteger(null); // false
Number.isSafeInteger(NaN); // false
Number.isSafeInteger(Infinity); // false
Number.isSafeInteger(-Infinity); // false

Number.isSafeInteger(3); // true
Number.isSafeInteger(1.2); // false
Number.isSafeInteger(9007199254740990); // true
Number.isSafeInteger(9007199254740992); // false

Number.isSafeInteger(Number.MIN_SAFE_INTEGER - 1); // false
Number.isSafeInteger(Number.MIN_SAFE_INTEGER); // true
Number.isSafeInteger(Number.MAX_SAFE_INTEGER); // true
Number.isSafeInteger(Number.MAX_SAFE_INTEGER + 1); // false

注意:使用Number.isSafeInteger()函数进行验证时,不要只验证运算结果,而要同时验证参与运算的每个值。

你还想继续看下去吗,反正我写博客都快写吐了,啥也不是
在这里插入图片描述

(7)Math 对象的扩展

ES6 在 Math 对象上新增了 17 个与数学相关的方法。所有这些方法都是静态方法,只能在 Math 对象上调用。

1,Math.trunc()

Math.trunc() : 用于去除一个数的小数部分,返回整数部分

Math.trunc(4.1) // 4
Math.trunc(4.9) // 4
Math.trunc(-4.1) // -4
Math.trunc(-4.9) // -4
Math.trunc(-0.1234) // -0

//对于非数值,Math.trunc内部使用Number方法将其先转为数值。
Math.trunc('123.456') // 123
Math.trunc(true) //1
Math.trunc(false) // 0
Math.trunc(null) // 0

//对于空值和无法截取整数的值,返回NaN。
Math.trunc(NaN);      // NaN
Math.trunc('foo');    // NaN
Math.trunc();         // NaN
Math.trunc(undefined) // NaN

//对于没有部署这个方法的环境,可以用下面的代码模拟。
Math.trunc = Math.trunc || function(x) {
  return x < 0 ? Math.ceil(x) : Math.floor(x);
};

2,Math.sign()

Math.sign方法用来判断一个数到底是正数、负数、还是零。

对于非数值,会先将其转换为数值。

Math.sign(-5) // -1           参数为负数,返回-1
Math.sign(5) // +1          参数为正数,返回+1
Math.sign(0) // +0           参数为 0,返回0;
Math.sign(-0) // -0           参数为-0,返回-0;
Math.sign(NaN) // NaN          其他值,返回NaN。

//如果参数是非数值,会自动转为数值。对于那些无法转为数值的值,会返回NaN。

Math.sign('')  // 0
Math.sign(true)  // +1
Math.sign(false)  // 0
Math.sign(null)  // 0
Math.sign('9')  // +1
Math.sign('foo')  // NaN
Math.sign()  // NaN
Math.sign(undefined)  // NaN

对于没有部署这个方法的环境,可以用下面的代码模拟。

Math.sign = Math.sign || function(x) {
  x = +x; // convert to a number
  if (x === 0 || isNaN(x)) {
    return x;
  }
  return x > 0 ? 1 : -1;
};

3,Math.cbrt()

Math.cbrt方法用于计算一个数的立方根。

Math.cbrt(-1) // -1
Math.cbrt(0)  // 0
Math.cbrt(1)  // 1
Math.cbrt(2)  // 1.2599210498948734

//对于非数值,Math.cbrt方法内部也是先使用Number方法将其转为数值。
Math.cbrt('8') // 2
Math.cbrt('hello') // NaN

//对于没有部署这个方法的环境,可以用下面的代码模拟。

Math.cbrt = Math.cbrt || function(x) {
  var y = Math.pow(Math.abs(x), 1/3);
  return x < 0 ? -y : y;
};

4,Math.clz32()

Math.clz32()方法将参数转为 32 位无符号整数的形式,然后返回这个 32 位值里面有多少个前导 0。

Math.clz32(0) // 32   0 的二进制形式全为 0,所以有 32 个前导 0;
Math.clz32(1) // 31   1 的二进制形式是0b1,只占 1 位,所以 32 位之中有 31 个前导 0;


Math.clz32(1000) // 22  1000 的二进制形式是0b1111101000,一共有 10 位,所以 32 位之中有 22 个前导 0。

Math.clz32(0b01000000000000000000000000000000) // 1
Math.clz32(0b00100000000000000000000000000000) // 2

对于小数,Math.clz32方法只考虑整数部分。

Math.clz32(3.2) // 30
 
Math.clz32(3.9) // 30

左移运算符(<<)与Math.clz32方法直接相关。

Math.clz32(0) // 32
 
Math.clz32(1) // 31
 
Math.clz32(1 << 1) // 30
 
Math.clz32(1 << 2) // 29
 
Math.clz32(1 << 29) // 2

对于空值或其他类型的值,Math.clz32方法会将它们先转为数值,然后再计算。

Math.clz32() // 32
Math.clz32(NaN) // 32
Math.clz32(Infinity) // 32
Math.clz32(null) // 32
Math.clz32('foo') // 32
Math.clz32([]) // 32
Math.clz32({}) // 32
Math.clz32(true) // 31

5.Math.imul()

Math.imul方法返回两个数以 32 位带符号整数形式相乘的结果,返回的也是一个 32 位的带符号整数。

Math.imul(2, 4)   // 8
Math.imul(-1, 8)  // -8
Math.imul(-2, -2) // 4

6,Math.fround()

Math.fround方法返回一个数的32位单精度浮点数形式。

对于32位单精度格式来说,数值精度是24个二进制位(1 位隐藏位与 23 位有效位),所以对于 -224 至 224 之间的整数(不含两个端点),返回结果与参数本身一致。

Math.fround(0)   // 0
Math.fround(1)   // 1
Math.fround(2 ** 24 - 1)   // 16777215

如果参数的绝对值大于 224,返回的结果便开始丢失精度。

Math.fround(2 ** 24)       // 16777216
Math.fround(2 ** 24 + 1)   // 16777216

Math.fround方法的主要作用,是将64位双精度浮点数转为32位单精度浮点数。如果小数的精度超过24个二进制位,返回值就会不同于原值,否则返回值不变(即与64位双精度值一致)。

// 未丢失有效精度
Math.fround(1.125) // 1.125
Math.fround(7.25)  // 7.25

// 丢失精度
Math.fround(0.3)   // 0.30000001192092896
Math.fround(0.7)   // 0.699999988079071
Math.fround(1.0000000123) // 1

对于 NaN 和 Infinity,此方法返回原值。对于其它类型的非数值,Math.fround 方法会先将其转为数值,再返回单精度浮点数。

Math.fround(NaN)      // NaN
Math.fround(Infinity) // Infinity

Math.fround('5')      // 5
Math.fround(true)     // 1
Math.fround(null)     // 0
Math.fround([])       // 0
Math.fround({})       // NaN
//对于没有部署这个方法的环境,可以用下面的代码模拟。

Math.fround = Math.fround || function (x) {
  return new Float32Array([x])[0];
};

7,Math.hypot()

Math.hypot方法返回所有参数的平方和的平方根。

Math.hypot(3, 4);        // 5
Math.hypot(3, 4, 5);     // 7.0710678118654755
Math.hypot();            // 0
Math.hypot(NaN);         // NaN
Math.hypot(3, 4, 'foo'); // NaN
Math.hypot(3, 4, '5');   // 7.0710678118654755
Math.hypot(-3);          // 3
//上面代码中,3 的平方加上 4 的平方,等于 5 的平方。

//如果参数不是数值,Math.hypot方法会将其转为数值。只要有一个参数无法转为数值,就会返回 NaN。

对数方法

ES6 新增了 4 个对数相关方法。

(1) Math.expm1()

Math.expm1(x)//返回的值等同于数学公式:e^x-1的预算结果

Math.expm1(-1) // -0.6321205588285577
Math.expm1(0)  // 0
Math.expm1(1)  // 1.718281828459045

(2)Math.log1p()

Math.log1p(x)方法返回1 + x的自然对数,即Math.log(1 + x)。如果x小于-1,返回NaN。

Math.log1p(1)  // 0.6931471805599453
Math.log1p(0)  // 0
Math.log1p(-1) // -Infinity
Math.log1p(-2) // NaN

(3)Math.log10()

Math.log10(x)返回以 10 为底的x的对数。如果x小于 0,则返回 NaN。

Math.log10(2)      // 0.3010299956639812
Math.log10(1)      // 0
Math.log10(0)      // -Infinity
Math.log10(-2)     // NaN
Math.log10(100000) // 5

(4)Math.log2()

Math.log2(x)返回以 2 为底的x的对数。如果x小于 0,则返回 NaN。

Math.log2(3)       // 1.584962500721156
Math.log2(2)       // 1
Math.log2(1)       // 0
Math.log2(0)       // -Infinity
Math.log2(-2)      // NaN
Math.log2(1024)    // 10
Math.log2(1 << 29) // 29

双曲函数方法

ES6 新增了 6 个双曲函数方法。

Math.sinh(x) //返回x的双曲正弦(sin)
Math.cosh(x) //返回x的双曲余弦(cos)
Math.tanh(x) //返回x的双曲正切(tan)

Math.asinh(x) //返回x的反双曲正弦(inverse hyperbolic sine)
Math.acosh(x) //返回x的反双曲余弦(inverse hyperbolic cosine)
Math.atanh(x) //返回x的反双曲正切(inverse hyperbolic tangent)

(8) 指数运算符

ES2016 新增了一个指数运算符(**)。**是右结合的,多个指数运算可以连用。

2 ** 2; // 4
2 ** 3; // 8

// 多个指数运算符连用
2 ** 3 ** 2; // 512

(9)BigInt 数据类型

由于JavaScript 所有数字都用64 位浮点数进行存储,数值的精度只能到 53 个二进制位,而且大于或等于2的1024次方的数值(小于或等于-2的1023次方的数值),JavaScript 无法表示,会返回Infinity(-Infinity)。

// 超过 53 个二进制位的数值,无法保持精度
Math.pow(2, 53) === Math.pow(2, 53) + 1; // true

// 超过 2 的 1024 次方的数值,无法表示
Math.pow(2, 1025); // Infinity

ES2020 引入了一种新的数据类型 BigInt,来解决这个问题。BigInt 只用来表示整数,没有位数的限制,任何位数的整数都可以精确表示。BigInt 类型的数据必须添加后缀n。

// Bigint数据后面加后缀n
const a = 21721416122353n;
const b = 153463491231309n;

// BigInt精度可以保证
a*b; // 3333444352624333556168350077n

Bigint类型的数据与普通数值是两种值,并不相等。

const c1 = 12n;
const c2 = 12;

typeof c1; // "bigint"
typeof c2; // "number"

// 两者不相等
c1 === c2; // false

BigInt 对象

JavaScript 原生提供BigInt对象,可以用作构造函数生成 BigInt 类型的数值。转换规则基本与Number()一致,将其他类型的值转为 BigInt。

BigInt(123) // 123n
BigInt('123') // 123n
BigInt(false) // 0n
BigInt(true) // 1n

BigInt()构造函数必须有参数,而且参数必须可以正常转为数值,下面的用法都会报错。

new BigInt() // TypeError
BigInt(undefined) //TypeError
BigInt(null) // TypeError
BigInt('123n') // SyntaxError    字符串123n无法解析成 Number 类型,所以会报错。
BigInt('abc') // SyntaxError

//参数如果是小数,也会报错。
BigInt(1.5) // RangeError
BigInt('1.5') // SyntaxError

BigInt 类型的+、-、*和**这四个二元运算符,与 Number 类型的行为一致。

除法运算“ / ”会舍去小数部分,返回一个整数。

12n + 12n; // 24n
2n * 2n; // 4n
12n - 2n; // 10n
2n ** 3n; // 8n

// 除法会舍去小数部分,返回整数
10n / 6n; // 1n

BigInt 不能与普通数值进行混合运算。

1n + 32; // Uncaught TypeError: Cannot mix BigInt and other types

有两个数值运算符不能用在Bigint数据类型的值上

  • 无符号右移>>>
  • 一元求正+

“>>>”是无符号右移,而Bigint数据类型的总是带符号的,所以该运算无意义;

而一元+运算符结果是返回Number的,会报错。

+ 12n; // Uncaught TypeError: Cannot convert a BigInt value to a number


//跟一元求正运算符一样,如果一个标准库函数的参数预期是Number 类型,
//但是得到的是一个 BigInt,就会报错。

Math.pow(2n, 2); // 报错
Math.abs(4n); // 报错
Math.sqrt(4n) // 报错

// 正确的写法
Math.sqrt(Number(4n)) // 2
//上面代码中,Math.sqrt的参数预期是 Number 类型,
//如果是 BigInt 就会报错,必须先用Number方法转一下类型,才能进行计算。

其他运算

BigInt 对应的布尔值,与 Number 类型一致,即0n会转为false,其他值转为true。

if (0n) {
  console.log('if');
} else {
  console.log('else');
}
// else

上面代码中,0n对应false,所以会进入else子句。

比较运算符(比如>)和相等运算符(==)允许 BigInt 与其他类型的值混合计算,因为这样做不会损失精度。

0n < 1 // true
0n < true // true
0n == 0 // true
0n == false // true
0n === 0 // false

BigInt 与字符串混合运算时,会先转为字符串,再进行运算。

'' + 123n // "123"

参考链接

ECMAScript 6 入门
作者:阮一峰

发布了23 篇原创文章 · 获赞 0 · 访问量 3117

猜你喜欢

转载自blog.csdn.net/weixin_45644335/article/details/104674123