es6+es7+es8学习笔记 教程

ES8

求幂运算符

console.info(3 ** 3)   //27

async await (异步)

        function doLater(n, x) {
            //必须要返回一个promise
            return new Promise((a) => {
                setTimeout(() => {
                    a(x)
                    console.info(x)
                }, n)
            })
        }
        async function basicDemo() {
            //使用await,会按顺序执行
            await doLater(1000, '1秒后我先执行')
            await doLater(1000, '再过1秒我执行')
        }
        basicDemo()


        //也可以在async函数中返回一个promise 然后就可以使用then了
        //函数声明: async function foo() {}
        //函数表达式: const foo = async function() {}
        //对象的方式: let obj = { async foo() {} }
        //箭头函数: const foo = async () => {}

ES7

ES6

promise (异步)

        function doPromise() {
            return new Promise((a, b) => {
                setTimeout(() => {
                    a("ok");
                }, 1000)
            })
        }

        doPromise().then(a => console.info(a))

Generator (按步执行)

//gennerator  与async await的区别是,前者需要手动执行,后者是自动向下执行的。
//方法名前要有星号,步骤前要有yield。执行.next()进行下一步
        function* onebyon() {
            console.info(0)
            yield 'first'
            console.info(1)
            yield 'secend'
            console.info(2)
            return 'ending'
        }

        var dos = onebyon()
        console.info(dos.next())
        console.info(dos.next())
        console.info(dos.next())
    //输出
    0
    {value: "first", done: false}
    1
    {value: "secend", done: false}
    2
    {value: "ending", done: true}
set数据结构
//该数据类型不会产生重复对象。
let a = new Set(['a', 'a', 'b'])
        a.add(9)
console.info(a)
//['a','b',9]
Set.prototype.constructor:构造函数,默认就是Set函数。
Set.prototype.size:返回Set实例的成员总数。
Set 实例的方法分为两大类:操作方法(用于操作数据)和遍历方法(用于遍历成员)。下面先介绍四个操作方法。

add(value):添加某个值,返回 Set 结构本身。
delete(value):删除某个值,返回一个布尔值,表示删除是否成功。
has(value):返回一个布尔值,表示该值是否为Set的成员。
clear():清除所有成员,没有返回值。

of循环

let set = new Set(['red', 'green', 'blue']);

for (let item of set.keys()) {
  console.log(item);
}
// red
// green
// blue

for (let item of set.values()) {
  console.log(item);
}
// red
// green
// blue

for (let item of set.entries()) {
  console.log(item);
}
// ["red", "red"]
// ["green", "green"]
// ["blue", "blue"]

猜你喜欢

转载自blog.csdn.net/lengyoumo/article/details/80400205
今日推荐