1-ES6知识

120 阅读1分钟

【8种数据类型】

undefined、null、Boolean、String、Number、Object、Symbol、BigInt

【箭头函数和普通函数的区别】

  1. 箭头函数不会创建自己的this,它会捕获自己在定义时所处的外层执行环境的this; 箭头函数继承而来的this指向永远不变,.call()/.apply()/.bind()无法改变箭头函数中this的指向
  2. 箭头函数没有自己的arguments,使用rest参数代替arguments对象
  3. 箭头函数没有原型prototype,不能作为构造函数使用
  4. 箭头函数不能用作Generator函数,不能使用yeild关键字

【Array.from()】

Array.from方法用于将类似数组的对象(array-like object)和可遍历(iterable)的对象(包括 ES6 新增的数据结构 Set 和 Map)转为真正的数组

let arrayLike = {
  '0': 'a',
  '1': 'b',
  '2': 'c',
  length: 3
};
// ES5的写法
var arr1 = [].slice.call(arrayLike); // ['a', 'b', 'c']
// ES6的写法
let arr2 = Array.from(arrayLike); // ['a', 'b', 'c']
Array.from('hello') // ['h', 'e', 'l', 'l', 'o']

【includes】 (可代替indexOf使用)

Array.prototype.includes方法返回一个布尔值,表示某个数组是否包含给定的值

[1, 2, 3].includes(2)     // true
[1, 2, 3].includes(4)     // false
[1, 2, NaN].includes(NaN) // true