关于es6:ES6学习-第六章-数值的扩展

7次阅读

共计 8440 个字符,预计需要花费 22 分钟才能阅读完成。

前言

本章介绍数值的扩大。新增了很多办法,有些不罕用的办法理解即可。
本章原文链接:数值的扩大

进制表示法

ES6 提供了二进制和八进制数值的新的写法,别离用前缀 0b(或0B)和0o(或0O)示意。
八进制就不再容许应用前缀 0 示意。
0b0o 前缀的字符串数值转为十进制,要应用 Number 办法。

console.log(Number('0b10'));  // 二进制 2
console.log(Number('0o10'));  // 八进制 8

数值分隔符

ES2021,容许 JavaScript 的数值应用 下划线(_)作为分隔符。
数值分隔符次要为了书写数值时减少数值的可读性,不是为了解决内部输出的数据,对于 JavaScript 外部数值的存储和输入,并没有影响。

留神:

  • 不能放在数值的最后面(leading)或最初面(trailing)。
  • 不能两个或两个以上的分隔符连在一起。
  • 小数点的前后不能有分隔符。
  • 迷信计数法外面,示意指数的 eE前后不能有分隔符。
  • 分隔符不能紧紧跟着进制的前缀
  • 字符串转数值的一些操作方法不反对数值分隔符

其它进制也能应用数值分隔符

const sample10 = 1000_1000_1000;
const sample2 = 0b1000_1000;
const sample8 = 0o1000_1000;

console.log(sample10);  // 十进制  100010001000
console.log(sample2);  // 二进制  136
console.log(sample8);  // 八进制  2097664

留神:Number()、parseInt()、parseFloat()不反对数字分隔符

数值的办法

Number.isFinite(), Number.isNaN()

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

  • Number.isFinite()用来查看一个数值是否为无限的数字(finite),即不是Infinity
  • Number.isNaN()用来查看一个数值是否为 NaN。

留神:
两个新办法与之前全局办法 isFiniteisNaN 有什么不同呢?

  • 而这两个新办法只对数值无效,
  • 传统办法先调用 Number() 将非数值的值转为数值,再进行判断,
isFinite(25) // true
isFinite("25") // true
Number.isFinite(25) // true
Number.isFinite("25") // false
Number.isFinite(Infinity); // false
Number.isFinite(-Infinity); // false

isNaN(NaN) // true
isNaN("NaN") // true
Number.isNaN(NaN) // true
Number.isNaN("NaN") // false

Number.parseInt(), Number.parseFloat()

ES6 将全局办法 parseInt()parseFloat(),移植到 Number 对象下面,行为 齐全放弃不变。次要是用于全局变量的模块化

  • parseInt() 函数解析字符串并返回整数。
  • parseFloat() 函数解析字符串并返回浮点数。
// ES5 的全局办法
const sampleInt = parseInt('11.11');
const sampleFloat = parseFloat('1a2b3c');

// ES6 的 Number 办法
const sampleInt1 = Number.parseInt('11.11');
const sampleFloat1 = Number.parseFloat('1a2b3c');

console.log(sampleInt, sampleFloat);  // 11, 1
console.log(sampleInt1, sampleFloat1);  // 11, 1

Number.isInteger()

Number.isInteger()办法用来判断给定的参数是否为整数。

留神:

  • 因为整数和浮点数采纳的是同样的贮存办法,所以 4 和 4.0 被视为同一个值。都为整数
  • 参数须要为数值,参数不是数值,Number.isInteger()间接返回false
  • 因为 JavaScript 数值精度最多能够达到 53 个二进制位,如果数值的精度超过这个限度,第 54 位及前面的位就会被抛弃,会导致 Number.isInteger 可能会误判。绝对值也是如此。
const sample1 = Number.isInteger(44);
const sample2 = Number.isInteger(44.00); // 相当于 44 
const sample3 = Number.isInteger('44');  // 非数值间接返回 false
const sample4 = Number.isInteger(44.0000000000000000987654321); // 误判为 true

console.log(sample1, sample2, sample3, sample4);  // true, true, false, true

数值新增常量

Number.EPSILON

ES6 在 Number 对象下面,新增一个极小的常量 Number.EPSILON。它示意 1 与大于 1 的最小浮点数之间的差。
对于 64 位浮点数来说,大于 1 的最小浮点数相当于二进制的1.00..001(小数点前面有间断 51 个零),这个值减去 1 之后,就等于 2 的 -52 次方。
Number.EPSILON 实际上是 JavaScript 可能示意的最小精度。
Number.EPSILON 本质是一个能够承受的最小误差范畴。

const sample = Number.EPSILON === Math.pow(2, -52);
console.log(sample); // 
const sample1 = Number.EPSILON;
console.log(sample1); // 
const sample2 = Number.EPSILON.toFixed(20);
console.log(sample2); // 

平安整数和 Number.isSafeInteger()

JavaScript 可能精确示意的整数范畴在 -2^53 到 2^53 之间(不含两个端点),超过这个范畴,无奈准确示意这个值。
ES6 引入了 Number.MAX_SAFE_INTEGERNumber.MIN_SAFE_INTEGER这两个常量,用来示意 -2^53 到 2^53 上上限。

const sample = Number.MAX_SAFE_INTEGER === 9007199254740991;
const sample1 = Number.MIN_SAFE_INTEGER === -Number.MAX_SAFE_INTEGER;
const sample2 = Number.MIN_SAFE_INTEGER === -9007199254740991;

console.log(sample,sample1,sample2);

Number.isSafeInteger()用来判断一个整数是否落在这个范畴之内,对于非整数,全副返回false

const sample = Number.isSafeInteger(44);   // 整数
const sample1 =Number.isSafeInteger(44.001);  // 非整数
const sample3 =Number.isSafeInteger(9007199254740990);
const sample3 =Number.isSafeInteger(9007199254740992);
console.log(sample,sample1,sample2,sample3); // true, false, true, false

Math 对象的扩大

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

  1. Math.trunc() – 取整。
  2. Math.sign() – 判断数字是负数、正数、还是零。
  3. Math.cbrt() – 计算一个数的立方根
  4. Math.clz32() – 计算一个数的 32 位二进制模式的前导 0 的个数。
  5. Math.imul() – 计算两个参数的类 C 32 位乘法的。
  6. Math.fround() – 返回一个数的 32 位单精度浮点数模式。
  7. Math.hypot() – 返回所有参数的平方和的平方根。
  8. Math.expm1() – 返回 ex – 1,x 为参数
  9. Math.log1p() – 返回参数 + 1 后的自然对数
  10. Math.log10() – 返回以 10 为底的参数对数
  11. Math.log2() – 返回以 2 为底的参数对数
  12. Math.sinh() – 函数返回一个数字 (单位为角度) 的双曲正弦值。
  13. Math.cosh() – 函数返回数值的双曲余弦函数。
  14. Math.tanh() – 函数将会返回一个数的双曲正切函数值。
  15. Math.asinh() – 函数返回给定数字的反双曲正弦值。
  16. Math.acosh() – 返回一个数字的反双曲余弦值。
  17. Math.atanh() – 函数返回一个数值反双曲正切值。

Math.trunc()

Math.trunc() 办法会将数字的小数局部去掉,只保留整数局部,是一个取整操作。
Math 中有三个办法:Math.floor()Math.ceil()Math.round(),也是用于取整操作。

  • Math.floor()向下取整;
  • Math.ceil()向上取整;
  • Math.round()进行四舍五入操作。
  • Math.trunc()去除小数局部,只保留整数局部。
const sample = Math.trunc(4.9); // 去掉小数位保留整数位
const sample1 = Math.trunc('4.4');  // 其它数据类型先调用 Number 转化为数值类型
const sample2 = Math.trunc('12.a'); // 不能正确转化为数值类型返回 NaN
console.log(sample, sample1,sample2); // 4, 4 ,NaN

Math.sign()

Math.sign()判断一个数到底是负数、正数、还是零。对于非数值,会先将其转换为数值。
5 种返回值, 别离是 1, -1, 0, -0, NaN. 代表的各是 负数, 正数, 正零, 负零, NaN

  • 参数为 负数,返回+1
  • 参数为 正数,返回-1
  • 参数为 0,返回0
  • 参数为-0,返回-0;
  • 其余值,返回NaN
const sample = Math.sign(-4);  // -1 正数
const sample1 = Math.sign(4);  // 1 负数
const sample2 = Math.sign(0);  // 0 0
const sample3 = Math.sign(-0);  // -0 -0
const sample4 = Math.sign('a');  // NaN  非数值

console.log(sample, sample1, sample2, sample3, sample4); // -1, 1, 0, -0, NaN

Math.cbrt()

在数学上 : 如果 x³=a,那么x 叫做 a 的立方根。
Math.cbrt()计算一个数的立方根

const sample = Math.cbrt(-1);
const sample1 = Math.cbrt(8);
const sample2 = Math.cbrt(0);  // 0 的立方根是 0
const sample3 = Math.cbrt(-0);
const sample4 = Math.cbrt('a');  // 非数值类型先调用 Number 转化为数值类型
console.log(sample, sample1, sample2, sample3, sample4); // -1, 2, 0, -0, NaN

Math.clz32()

Math.clz32()函数返回参数转化为 32 位无符号整数数字二进制 结尾的 0 的个数,
对于空值或其余类型的值,Math.clz32办法会将它们先转为数值,而后再计算。

留神

  • Math.clz32()对于小数,只思考整数局部
  • << 运算符把【要位移的数字】的所有位向左移【位移位数】指定的位数。
  • result =【要位移的数字】<<【位移位数】
const sample = Math.clz32();  // 空转化为数值为 0 
const sample1 = Math.clz32(1 << 29);  // 左位移运算符扭转
const sample2 = Math.clz32(44.7); // 只思考整数局部
const sample3 = Math.clz32(true);  // 转化为数值为 1 
const sample4 = Math.clz32('a');  // 非数值类型先调用 Number 转化为数值类型
console.log(sample, sample1, sample2, sample3, sample4); // 32, 2, 26, 31, 32

Math.imul()

Math.imul()办法将两个参数别离转换为 32 位整数,相乘后返回 32 位的带符号整数。
JavaScript 有精度限度,使得超过 2 的 53 次方的值无奈准确示意。Math.imul()办法能够返回正确的低位数值。

const sample = Math.imul(-1, 8.9);  // 参数有小数会先转化为整数
const sample1 = Math.imul(0xffffffff, 5); 
//  上面的参数它们的乘积超过了 2 的 53 次方也能正确显示
const sample2 = Math.imul(0x7fffffff, 0x7fffffff); 

console.log(sample, sample1, sample2); // -8, -5, 1

Math.fround()

Math.fround() 能够将任意的数字转换为 32 位单精度浮点数模式。
JavaScript 外部应用 64 位的双浮点数字,反对很高的精度。对于 32 位单精度格局来说,数值精度是 24 个二进制位(1 位暗藏位与 23 位无效位)

留神

  • 如果参数的绝对值大于 224,返回的后果便开始失落精度。
  • 对于 NaNInfinity,此办法返回原值
  • 对于其它非数值,Math.fround 办法先将其转为数值,再返回单精度浮点数。
const sample = Math.fround(99); 
const sample1 = Math.fround(0.7); // 失落精度
const sample2 = Math.fround('5');
const sample3 = Math.fround(Infinity);

console.log(sample, sample1, sample2, sample3); 
// 输入 99, 0.699999988079071, 5, Infinity

Math.hypot()

Math.hypot()函数返回所有参数的平方和的平方根。

const sample = Math.hypot(3, 4); // 2*2 + 2*2 的平方根
const sample1 = Math.hypot(); // 0 空转化为数值为 0 
const sample2 = Math.hypot('-9');
const sample3 = Math.hypot(Infinity);  // 非数值类型先转化为数值类型
const sample4 = Math.hypot(1, 2, 'a'); // 只有有一个参数无奈转为数值,就会返回 NaN。console.log(sample, sample1, sample2, sample3, sample4); // 5, 0, 9, Infinity, NaN 

对数办法

Math.expm1()

Math.expm1()返回 ex – 1,即 Math.exp(x) - 1 其中 x 是该函数的参数, e 是自然对数的底数

const sample = Math.expm1(-38);
const sample1 = Math.expm1(0);
const sample2 = Math.expm1(1);
const sample3 = Math.expm1('a');

console.log(sample, sample1, sample2, sample3); // -1, 0, 1.718281828459045, NaN

Math.log1p()

Math.log1p()办法返回参数 + 1 后的自然对数,(底为 e), 即Math.log(1 + x)

const sample = Math.log1p(-2); // 参数小于 -1 返回 NaN
const sample1 = Math.log1p(-1); // -1 + 1 = 0 返回 - Infinity 0 没有对数
const sample2 = Math.log1p(0); // 0 + 1 = 1 1 的对数是 0
const sample3 = Math.log1p('a');

console.log(sample, sample1, sample2, sample3); // NaN,-Infinity,0,NaN

Math.log10()

Math.log10()返回以 10 为底的参数对数

const sample = Math.log10(-2); // 参数小于 0 返回 NaN
const sample1 = Math.log10(1); // 1 的对数是 0
const sample2 = Math.log10('10'); // 转化为数值类型
const sample3 = Math.log10('a');

console.log(sample, sample1, sample2, sample3); // NaN, 0, 1, NaN

Math.log2()

Math.log10()Math.log2() 相似,一个以 10 为底,一个以 2 为底
Math.log2() 返回以 2 为底的参数对数

const sample = Math.log2(-2); // 参数小于 0 返回 NaN
const sample1 = Math.log2(1); // 1 的对数是 0
const sample2 = Math.log2('1024'); // 转化为数值类型
const sample3 = Math.log2('a');

console.log(sample, sample1, sample2, sample3); // NaN, 0, 10, NaN

双曲函数办法

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

  1. Math.sinh() 函数返回一个数字 (单位为角度) 的双曲正弦值。
  2. Math.cosh() 函数返回数值的双曲余弦函数, 可用 constant e 示意。
  3. Math.tanh() 函数将会返回一个数的双曲正切函数值。
  4. Math.asinh() 函数返回给定数字的反双曲正弦值。
  5. Math.acosh() 返回一个数字的反双曲余弦值。
  6. Math.atanh() 函数返回一个数值反双曲正切值。

BigInt 数据类型

形容

ES2020 引入了一种新的数据类型 BigInt,这是 ECMAScript 的第八种数据类型。
BigInt 只用来示意整数,没有位数的限度,任何位数的整数都能够准确示意。
BigInt 数据类型的目标是比 Number 数据类型反对的范畴更大的整数值。

留神

  • BigInt 也能够应用各种进制示意,都要加上后缀 n
  • BigIntNumber 数值的类型不同。
  • BigInt 除一元加号 (+) 运算符外,BigInt 能够应用所有运算符。
  • BigInt 也能够转化为其它数据类型。
  • BigInt 不能与一般数值进行混合运算。
const sample = 99999999999999999999n; // 能够示意任意长度的整数
const sample1 = 999n + 999n * 99n / 99n - 99n; // 能够应用除一元加号外所有运算符
const sample2 = 0o777n + 0b1101n;  // 能够应用各种进制来示意
const sample3 = String(1n); // 转化为其余类型数据 n 会隐没

console.log(sample); // 99999999999999999999n
console.log(sample1); // 1899n
console.log(sample2); // 524n
console.log(sample3); // 1

const sample4 = 10n + 10; // 间接报错 不能与一般数值进行混合运算。

BigInt 函数

JavaScript 原生提供 BigInt 函数,将其余类型的值转为 BigInt 类型。与 Number() 统一

留神

  • 参数不能为空。
  • 参数不能为小数。
  • 参数必须能失常转化为数值。
const sample = BigInt(44);  
const sample1 = BigInt('490');  // 能够正确转换

console.log(sample); // 44n
console.log(sample1); // 490n

// 上面全副报错
const sample2 = BigInt(undefined);
const sample3 = BigInt('44a'); // 转化为数值为 NaN
const sample4 = BigInt(1.1); // 参数为小数报错
const sample5 = BigInt();  // 为空
正文完
 0