es6新增语法总结
-
let(变量)和const(常量)
相比于var:
1.拥有了块级作用域
2.必须先申明在使用
3.let变量不会压入window中是独立的 -
类
class Animal {
// 构造函数,实例化的时候将会被调用,如果不指定,那么会有一个不带参数的默认构造函数.
constructor(name,color) {
this.name = name;
this.color = color;
}
// toString 是原型对象上的属性
toString() {
console.log('name:' + this.name + ',color:' + this.color);
}
}
var animal = new Animal('dog','white');//实例化Animal
animal.toString();
console.log(animal.hasOwnProperty('name')); //true
console.log(animal.hasOwnProperty('toString')); // false
console.log(animal.__proto__.hasOwnProperty('toString')); // true
class Cat extends Animal {
constructor(action) {
// 子类必须要在constructor中指定super 函数,否则在新建实例的时候会报错.
// 如果没有置顶consructor,默认带super函数的constructor将会被添加、
super('cat','white');
this.action = action;
}
toString() {
console.log(super.toString());
}
}
var cat = new Cat('catch')
cat.toString();
// 实例cat 是 Cat 和 Animal 的实例,和Es5完全一致。
console.log(cat instanceof Cat); // true
console.log(cat instanceof Animal); // true
- 解构赋值
- 箭头函数
- 字符串魔板字面量 -默认参数 -导出
exporting in Common.js——moudle.export
exporting in es6——export or export default
- 新增函数
1.Array.form()——把伪数组转变为数组
2.find()方法和findIndex()方法
3.includes()方法、startsWith()方法和endsWith()方法
console.log('hello world'.includes('a', 2)); // false 从位置2开始查找a,没有找到
console.log('hello world'.includes('a')); // true
console.log('hello world'.startsWith('b')); // 未指定位置,看开头是否是a,返回true
console.log('hello world'.startsWith('b', 2)); // 指定位置的字符是b,返回true
console.log('hello world'.endsWith('c')); // 未指定位置,结尾是c,返回true
console.log('hello world'.endsWith('c', 9)); // 指定位置的字符是c,返回true
4.repeat()方法
5.set()方法(用于数组去重)
6.Map()函数——Object提供了‘字符串-值’的键值对模式,es6的Map提供了‘值-值’的键值对模式
size :Map的长度
set :给 Map 新增一对键值
get : 获取某个键的值
has :判断是否存在指定的键
7.promises——解决回调地狱
8.async await
9.reduce函数
接收一个函数(必须)和一个初始值(可选),该函数接收两个参数:
第一个参数是上一次reduce处理的结果
第二个参数是数组中要处理的下一个元素
reduce() 会从左到右依次把数组中的元素用reduce处理,并把处理的结果作为下次reduce的第一个参数。如果是第一次,会把前两个元素作为计算参数,或者把用户指定的初始值作为起始参数
*详情可查看文档:*https://www.jianshu.com/p/b6c882e59b07#replacing-iifes-with-blocks