ES6——7.1.math的扩展

Math.trunc()

  1. 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
  1. 对于非数值,Math.trunc内部使用Number方法将其先转为数值。

  2. 对于空值和无法截取整数的值,返回NaN

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

    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.cbrt()

  1. Math.cbrt()方法用于计算一个数的立方根。
  2. 对于非数值,Math.cbrt()方法内部也是先使用Number()方法将其转为数值。

Math.clz32()

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

Math.imul()

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

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

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

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。

对数方法

双曲函数方法

指数运算符

ES2016 新增了一个指数运算符(**)。

2 ** 2 // 4
2 ** 3 // 8
7.1.10、迭代器遍历器(Iterator)就是一种机制。它是一种接口,为各种不同的数据结构提供统一的访问机制。任何数据结构只要部署 Iterator 接口,就可以完成遍历操作。ES6 创造了一种新的遍历命令 for…of 循环,Iterator 接口主要供 for…of 消费,原生具备 iterator 接口的数据:ArrayArgumentsSetMapStringTypedArrayNodeList注意:需要自定义遍历数据的时候,要想到迭代器工作原理:创建一个指针对象,指向当前数据结构的起始位置第一次调用对象的 next 方法,指针自动指向数据结构的第一个成员接下来不断调用 next 方法,指针一直往后移动,直到指向最后一个成员每调用 next 方法返回一个包含 value 和 done 属性的对象案例演示:遍历数组//声明一个数组const xiyou = ["唐僧", "孙悟空", "猪八戒", "沙僧"];//使用 for...of 遍历数组for (let v of xiyou) { console.log(v);}console.log("===============");//获取迭代器对象let iterator = xiyou[Symbol.iterator]();//调用对象的next方法console.log(iterator.next());console.log(iterator.next());console.log(iterator.next());console.log(iterator.next());console.log(iterator.next());12345678910111213141516案例演示:自定义遍历数据//声明一个对象const banji = { name: "五班", stus: [ "张三", "李四", "王五", "小六" ], [Symbol.iterator]() { //索引变量 let index = 0; let _this = this; return { next: function () { if (index < _this.stus.length) { const result = {value: _this.stus[index], done: false}; //下标自增 index++; //返回结果 return result; } else { return {value: undefined, done: true}; } } }; }}//遍历这个对象for (let v of banji) { console.log(v);}1234567891011121314151617181920212223242526272829303132337.1.11、生成器生成器函数是 ES6 提供的一种异步编程解决方案,语法行为与传统函数完全不同。7.1.11.1、生成器函数使用代码说明:* 的位置没有限制生成器函数返回的结果是迭代器对象,调用迭代器对象的 next 方法可以得到 yield 语句后的值yield 相当于函数的暂停标记,也可以认为是函数的分隔符,每调用一次 next 方法,执行一段代码next 方法可以传递实参,作为 yield 语句的返回值function * gen() { /*代码1开始执行*/ console.log("代码1执行了"); yield "一只没有耳朵"; /*代码2开始执行*/ console.log("代码2执行了"); yield "一只没有尾巴"; /*代码3开始执行*/ console.log("代码3执行了"); return "真奇怪";}let iterator = gen();console.log(iterator.next());console.log(iterator.next());console.log(iterator.next());console.log("===============");//遍历for (let v of gen()) { console.log(v);}123456789101112131415161718192021227.1.11.2、生成器函数参数function * gen(arg) { console.log(arg); let one = yield 111; console.log(one); let two = yield 222; console.log(two); let three = yield 333; console.log(three);}//执行获取迭代器对象let iterator = gen('AAA');console.log(iterator.next());//next方法可以传入实参console.log(iterator.next('BBB'));console.log(iterator.next('CCC'));console.log(iterator.next('DDD'));1234567891011121314151617187.1.11.3、生成器函数实例案例演示:1s后控制台输出 111,2s后输出 222,3s后输出 333function one() { setTimeout(() => { console.log(111); iterator.next(); }, 1000)}function two() { setTimeout(() => { console.log(222); iterator.next(); }, 2000)}function three() { setTimeout(() => { console.log(333); iterator.next(); }, 3000)}function * gen() { yield one(); yield two(); yield three();}//调用生成器函数let iterator = gen();iterator.next();123456789101112131415161718192021222324252627282930案例演示:模拟获取 ,用户数据 ,订单数据 ,商品数据function getUsers() { setTimeout(() => { let data = "用户数据"; iterator.next(data); }, 1000);}function getOrders() { setTimeout(() => { let data = "订单数据"; iterator.next(data); }, 1000);}function getGoods() { setTimeout(() => { let data = "商品数据"; iterator.next(data); }, 1000);}function * gen() { let users = yield getUsers(); console.log(users); let orders = yield getOrders(); console.log(orders); let goods = yield getGoods(); console.log(goods);}//调用生成器函数let iterator = gen();iterator.next();123456789101112131415161718192021222324252627282930313233
最新发布
04-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值