学习随记 - LiangYiDong/study GitHub Wiki

  1. **async 与 await的配合使用**
   async buildLink(){
       for(var title of this.titList) {
           console.log(title.index, 'before')
           var res = await this.createLink(xxxxx);
           console.log(title.index, 'after')
       }
       //处理1:(需要等第一步完成,才执行下一步)
       await getBrandPms();
       await getCoupon();
       //处理2:(两个请求同步发出,等两个请求都有回应再执行下一步)
       var [getBrandPmsRes, getCouponRes] = await Promise.all([getBrandPms(), getBrandPms()]);
   };
   //同上面的处理2意思一样
   async function correctDemo() {
        let p1 = sleep(1000);
        let p2 = sleep(1000);
        let p3 = sleep(1000);
        await Promise.all([p1, p2, p3]);
        console.log('clear the loading~');
    }
    correctDemo();// clear the loading
  1. **for in 与 for of**
 //for in是ES5标准,遍历key. 
 //for of是ES6标准,遍历value.
    for (var key in arr){
        console.log(arr[key]);
    }
    for (var value of arr){
        console.log(value);
    }
  1. **面向切片编程的思想**
   //可以在封装好的一个方法的之前和之后做自己想做的处理,形成一个拦截器
   var sayHello = function(a,b,c) {
       console.log('hello')
       // ...
   }
   //执行sayHello方法,在输出hello之前和之后输出其他的东西
   var oldSayHello = sayHello;
   sayHello = function(a,b,c) {
       console.log('before hello')
       oldSayHello.call(this, a, b,c)
       console.log('after hello')
   }
   sayHello();
   //输出的内容:
   before hello
   hello
   after hello
  1. **运用对象来做去重处理**
   var arr = [{vid: 11,name:"dedn"},{vid:22,name:"fdee"},{vid:22,name:"dfde"}]
   var vids = {};
   var arrAfter = arr.map((item,index) => {
      if(vids[item.vid]) return;
      vids[item.vid] = true;
      return item;
   }).filter(item => item);
   console.log(arrAfter);
   //filter(item => item) 过滤为空的数据
⚠️ **GitHub.com Fallback** ⚠️