0
点赞
收藏
分享

微信扫一扫

es6知识点学习总结(2)

8.Set

类似Array的新的数据结构
Set实例的成员都是唯一的,不重复的。这个特性可以轻松实现数组去重。

let array = [1,2,3,2,3]

let set = new Set(array);

console.log(set);           //[1,2,3]

let o1 = {age:11}

let o2 = {age:12}

let set = new Set([o1,o2]);

console.log(set);           //结果里面有两个对象吧,因为对象的内存地址不一样

let o2 = o1;

let set = new Set([o1,o2]);

console.log(set);           //结果里面有一个对象,对象的内存地址一样

属性

    .size 返回set集合的大小

方法

    .add()<相当于数组的push()> .delete(value) .has(value) .clear()清除数组

9.Map

MapES6引入的一种类似Object的新的数据结构,Map可以理解为是Object的超集,打破了以传统键值对形式定义对象,对象的key不再局限于字符串,也可以是Object

let map1 = new Map()

let obj = {name:'tiger'}

map1.set(obj,'hello')     //设置键值对

console.log(map1) // {{name:tiger}:hello}

map1.set(1,'hello1')

map1.set(2,'hello2')

console.log(map1) // {{name:tiger}:hello,1 => "hello1", 2 => "hello2"}

//遍历键值对

for(let item of map1){

    console.log(item);

}

//遍历键

for(let item of map1.keys()){

    console.log(item);

}

//遍历值

for(let item of map1.value()){

    console.log(item);

}

10.Promise

function ajaxPromise() {

    return new Promise(function(reslove,reject) {

        console.log("正在执行...");

        if(true) {

            reslove("success")

        }else{

            reject("fail")

        }

     })

 }

ajaxPromise().then(value => {

    console.log(value);

}).catch(err => {

    console.log(err);

})

11.Generator函数

执行Generator函数会返回一个遍历器对象,每一次Generator函数里面的yield都相当一次遍历器对象的next()方法,并且可以通过next(value)方法传入自定义的value,来改变Generator函数的行为。
定义函数的时候需要加上星号,例:function test(){}
函数体里面包含yield(暂停执行的标志),代码运行到yield的时候便会暂停执行,然后通过.next(),让代码继续执行,直到下一个yield或者函数体结束符。

function* demo() {

  console.log(1)

  yield 111

  console.log(2)

  yield 222

  console.log(3)

  return 333

}

let g = demo()

let val1 = g.next()

console.log('val1',val1)

// 打印 1val1 = {value: 111, done: false}

let val2 = g.next()

// 打印 2val2 = {value: 222, done: false}

console.log('val2',val2)

let val3 = g.next()

console.log('val3',val3)

12.async 函数

async函数就是将 Generator 函数的星号(*)替换成async,将yield替换成await,仅此而已。

const gen = function* () {

  const f1 = yield readFile('/etc/fstab');

  const f2 = yield readFile('/etc/shells');

  console.log(f1.toString());

  console.log(f2.toString());

};

const asyncReadFile = async function () {

  const f1 = await readFile('/etc/fstab');

  const f2 = await readFile('/etc/shells');

  console.log(f1.toString());

  console.log(f2.toString());

};

async函数返回一个 Promise 对象,可以使用then方法添加回调函数。当函数执行的时候,一旦遇到await就会先返回,等到异步操作完成,再接着执行函数体内后面的语句。

async function t(){

    var a = new Promise(

        function (resolve, reject) {

            console.log(1);

            resolve(9);

            console.log(2);

        })

    var b = new Promise(

        function (resolve, reject) {

            console.log(3);

            resolve(8);

            console.log(4);

        })

    console.log(5);

    b=await b;

    a=await a;

    console.log(b);

    console.log(a);

    console.log(6);

}

t();

console.log(7);

// 123457896

13.

使用import取代require

// bad

const moduleA = require('moduleA');

const func1 = moduleA.func1;

const func2 = moduleA.func2;

// good

import { func1, func2 } from 'moduleA';

// commonJS的写法

var React = require('react');

var Breadcrumbs = React.createClass({

  render() {

    return <nav />;

  }

});

module.exports = Breadcrumbs;

// ES6的写法

import React from 'react';

class Breadcrumbs extends React.Component {

  render() {

    return <nav />;

  }

};

export default Breadcrumbs;

举报

相关推荐

0 条评论