字符串
-
模板字符串
hello ${name}. -
新增方法
includes():返回布尔值,表示是否找到了参数字符串。 startsWith():返回布尔值,表示参数字符串是否在原字符串的头部。 endsWith():返回布尔值,表示参数字符串是否在原字符串的尾部。 repeat() :返回字符串,表示重复字符串几次
函数 (rest 、箭头)
- rest 参数搭配的变量是一个
数组,该变量将多余的参数放入数组中。function rainy( ...rest){ } // arguments变量的写法 function sortNumbers() { return Array.prototype.slice.call(arguments).sort(); } // rest参数的写法 const sortNumbers = (...numbers) => numbers.sort(); - 箭头函数
-
函数体内的this对象,就是
定义时所在的对象,而不是使用时所在的对象。 -
不可以当作
构造函数,也就是说,不可以使用new命令,否则会抛出一个错误。 -
不可以使用
arguments对象,该对象在函数体内不存在。如果要用,可以用rest参数代替。 -
不可以使用
yield命令,因此箭头函数不能用作Generator函数。
-
数组的扩展 (... / Set / Map / Array.from )
扩展运算符
..., 好比rest参数运算
console.log(...[1, 2, 3]) // 1 2 3
// ES5 的合并数组
arr1.concat(arr2, arr3);
// [ 'a', 'b', 'c', 'd', 'e' ]
// ES6 的合并数组
[...arr1, ...arr2, ...arr3]
// [ 'a', 'b', 'c', 'd', 'e' ]
Set 和 Map
- set 对象类似于数组,且成员的值都是
唯一的 - Map 对象是键值对集合,和JSON对象类似,但是key不仅可以字符串还可以是对象。
return [...new Set(arr)] //去重
let map = new Map([
[1, 'one'],
[2, 'two'],
[3, 'three'],
]);
- set实例 : `size add clear delete entries forEach has keys values `
- map实例: `size add clear delete entries forEach has keys values `
Array.from
- Array.from方法用于将两类对象转为真正的数组:类似数组的对象(array-like object)和可遍历(iterable)的对象(包括 ES6 新增的数据结构 Set 和 Map)。
- 只要是部署了 Iterator 接口的数据结构,Array.from都能将其转为数组。
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']
区别 forEach filter some map
map():返回一个新的Array,每个元素为调用func的结果
filter():返回一个符合func条件的元素数组
some():返回一个boolean,判断是否有元素是否符合func条件
every():返回一个boolean,判断每个元素是否符合func条件
forEach():没有返回值,只是针对每个元素调用func
reduce():有返回值,重点是计算数组,返回一个值
对象
- 简写
let birth = '2000/01/01';
const Person = {
name: '张三',
//等同于birth: birth
birth,
// 等同于hello: function ()...
hello() { console.log('我的名字是', this.name); }
};
module.exports = { getItem, setItem, clear };
// 等同于
module.exports = {
getItem: getItem,
setItem: setItem,
clear: clear
};
- 属性的遍历
- for---in
- Object.keys(obj)
- ...
- 新增方法
- Object.assign()
用于对象的合并将源对象(source)的所有可枚举属性,复制到目标对象(target)。 const target = { a: 1 }; const source1 = { b: 2 }; const source2 = { c: 3 }; Object.assign(target, source1, source2); //后面属性会覆盖前面 target // {a:1, b:2, c:3} const target = { a: { b: 'c', d: 'e' } } //注意 const source = { a: { b: 'hello' } } Object.assign(target, source) // { a: { b: 'hello' } } 而不是 { a: { b:'hello', d: 'e'}}