zoukankan      html  css  js  c++  java
  • 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

       多行字符串/模板变量

       解构赋值

       块级作用域

       函数默认参数

       箭头函数

     
  • 相关阅读:
    你自己不优秀,就算认识再多优秀人又有何用
    史玉柱和他老同学的一段故事
    哪有雪中送碳,都是锦上添花
    围城之困
    心已死,梦前行
    一位销售高手逼单经历!
    Python--函数return多个值
    Python--内置函数
    Python--小程序
    Python--递归
  • 原文地址:https://www.cnblogs.com/zhuMother/p/13256291.html
Copyright © 2011-2022 走看看