es6标准入门教程学习(二)

5.正则的扩展

//正则个人使用不多,这部分先跳过,到时候找点例子和文章单独总结

6.数值的拓展

1.八进制与二进制表示法

0b

0B

0o

0O

对二进制八进制字符串转换成十进制要使用Number方法

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

2.Number

Number.isFinite()判断数值是否有限,注意判断对象是数值,不是infinity
Number.isNaN()        就isNAN
Number.parseInt()int
Number.parseFloat()float
Number.isInteger()1.数值精度只能小于54位  2.数值绝对值小于Number.MIM_VALUE(5E-324)
Number.EPSILON用于设置误差范围
Number.isSafeInteger

1.判断数字是否在js精确表示范围中 2 53

2.判断时,应验证参与演算的每一个值

3.当一个数字超越了精度范围,以9007199254740992存储(Math.pow(2,53))

3.Math

Math.trunc()1.取整  2.非数值,先number转3.无法正确操作的值 NAN
Math.sign()1.判断数字类型,正数+1,负数-1,00,-0-0,其他nan
Math.cbrt()1.立方根
Math.clz32()1.将参数转换成32位无符号整数都形式,返回有多少个前导零

Math.imul()

Math.fround()32位单精度浮点数形式。 -2 24 至 2 24 
Math.hypot()所有参数平方和的平方根
Math.expm1返回 ex - 1
Math.log1pmath.log(1+x)

4.指数运算符

2 ** 2 // 4
2 ** 3 // 8
// 相当于 2 ** (3 ** 2)
2 ** 3 ** 2

5.BigInt大整数

只用来表示证书,没有位数的限制,必须添加后缀n,可以使用各种进制表示,也是必须+n,

bigint和普通数字并不相等,

7.函数的拓展

1.函数默认值 (和变量解构和let暂时性死区和块级作用域有关)

2.rest参数(...变量名) 之后不能有其他参数

3.严格模式

4.name属性

5.箭头函数  再看几遍这个(1/n)

不可以使用yield命令,因此箭头函数不能用作 Generator 函数。

不可以使用arguments对象,该对象在函数体内不存在。如果要用,可以用 rest 参数代替。

6.尾调用  :b是a的内部函数,如果b在a函数最后一步被调用,且不需要a中变量, 就可以尾调用优化,b替代a的调用帧

function Fibonacci2 (n , ac1 = 1 , ac2 = 1) {
  if( n <= 1 ) {return ac2};
  return Fibonacci2 (n - 1, ac2, ac1 + ac2);
}
Fibonacci2(100) // 573147844013817200000
Fibonacci2(1000) // 7.0330367711422765e+208
Fibonacci2(10000) // Infinity

柯里化

function currying(fn, n) {
  return function (m) {
    return fn.call(this, m, n);
  };
}
function tailFactorial(n, total) {
  if (n === 1) return total;
  return tailFactorial(n - 1, n * total);
}
const factorial = currying(tailFactorial, 1);
factorial(5) // 120

尾递归优化的实现(要用脑子,先浅看)

7.toString() 返回函数代码本身

8.数组的扩展

1.扩展运算符 ...  将一个数组变成参数序列  (apply方法 将数组转换成函数参数)

// ES5 的写法
Math.max.apply(null, [14, 3, 77])
// ES6 的写法
Math.max(...[14, 3, 77])
// 等同于
Math.max(14, 3, 77);

// ES5
new (Date.bind.apply(Date, [null, 2015, 1, 1]))
// ES6
new Date(...[2015, 1, 1]);??

2.排序

const arr = [
  'peach',
  'straw',
  'apple',
  'spork'
];
const stableSorting = (s1, s2) => {
  if (s1[0] < s2[0]) return -1;
  return 1;
};
arr.sort(stableSorting)
// ["apple", "peach", "straw", "spork"]

插入排序、合并排序、冒泡排序等都是稳定的,堆排序、快速排序等是不稳定的

3.

Array.from()将类数组对象转换成数组
Array.of()方法用于将一组值,转换为数组。

copyWithin()

在数组内部,将指定位置成语复制到其他位置,覆盖原有成员,返回当前数组
find()

他的参数是回调函数,所有数组成员依次执行回调函数,直到找到第一个返回值为true,or undefined

接受第二个参数,用来绑定回调函数的this对象

可以发现NaN(indexof不可以)

findIndex()

返回位置or-1

接受第二个参数,用来绑定回调函数的this对象

可以发现NaN(indexof不可以)

fill()

用定值补充数组

(定值,起始位置,终止位置)

空数组初始化

如果填充的类型为对象,那么被赋值的是同一个内存地址的对象,而不是深拷贝对象。

entries()['a']  return 0,'a'
keys()['a']  return 0
values()['a']  return 'a'
includes()
flat()

将嵌套数组拉平变成一维数组

如果原数组有空位,就会被跳过

flatMap()

flat()+map()

只能展开一层数组

Array.from({ length: 2 }, () => 'jack')
// ['jack', 'jack']

[1, 2, 3, 4, 5].copyWithin(0, 3)
// [4, 5, 3, 4, 5]

[1, 4, -5, 10].find((n) => n < 0)
// -5

function f(v){
  return v > this.age;
}
let person = {name: 'John', age: 20};
[10, 12, 26, 15].find(f, person);    // 26

let arr = new Array(3).fill({name: "Mike"});
arr[0].name = "Ben";
arr
// [{name: "Ben"}, {name: "Ben"}, {name: "Ben"}]
let arr = new Array(3).fill([]);
arr[0].push(5);
arr
// [[5], [5], [5]]

let letter = ['a', 'b', 'c'];
let entries = letter.entries();
console.log(entries.next().value); // [0, 'a']
console.log(entries.next().value); // [1, 'b']
console.log(entries.next().value); // [2, 'c']

// 相当于 [[2, 4], [3, 6], [4, 8]].flat()
[2, 3, 4].flatMap((x) => [x, x * 2])
// [2, 4, 3, 6, 4, 8]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值