ES6的模块化/class/promise及其他新的特性

1.模块化

语法:import export     

export {}

export default {}

import

import {} 

环境:babel编译ES6语法,模块化可用webpack和rollup

2.class可以看作就是构造函数,class是一个语法糖

class MathHandle {
    constructor(x, y) {
        this.x = x
        this.y = y
    }
    add() {
        return this.x + this.y
    }
}

const m = new MathHandle(1, 2)

console.log(typeof MathHandle)  // 'function'
console.log(MathHandle.prototype.constructor === MathHandle)  // true
console.log(m.__proto__ === MathHandle.prototype)  // true

3.promise:

  new Promise实例,而且要return

  new Promise时要传入函数,函数又resolve reject两个参数

  成功时执行resolve(),失败的时候执行reject()  

function loadImg(src) {
    var promise = new Promise(function (resolve, reject) {
        var img = document.createElement('img')
        img.onload = function () {
            resolve(img)
        }
        img.onerror = function () {
            reject('图片加载失败')
        }
        img.src = src
    })
    return promise
}

4.其他的es6特性

   let/const

   多行字符串/模板变量

   解构赋值

   块级作用域

   函数默认参数

   箭头函数

 
原文地址:https://www.cnblogs.com/zhuMother/p/13256291.html