zoukankan      html  css  js  c++  java
  • 关于ES6新增的东西(二)

    六、原生Promise

    就是一个对象,用来传递异步操作的数据(消息)
    
    pending(等待、处理中)—> Resolve(完成、fullFilled)
                  —> Rejected(拒绝、失败)

    ES6:

    var p1=new Promise(function(resolve,reject){
        //resolve  成功了
        //reject    失败了
            });
    
    var p1=new Promise(function(resolve,reject){
        if(异步处理成功了){
            resolve(成功数据)
        }else{
            reject(失败原因)
        }
            });
    
    p1.then(成功(resolve),失败(reject))    √
    --------------------------------------------
    p1.catch——用来捕获错误

    七、新方法

    1》箭头函数

    根据参数个数不同,分这几种情况:
    () => { … } // 零个参数用 () 表示
    x => { … } // 一个参数可以省略 ()
    (x, y) => { … } // 多参数不能省略 ()

    eg:

    /*function show(a){
    return a;
    }
    var s=show(12);
    alert(s);*/
    
    var show=(a,b)=>a+b;
    
    var s=show(12,5);

     2》复制数组

       arr2.from(arr)//这样不用再使用for循环了

    ​      arr2 = [...arr]

    ​ 3》for of循环

    ​ 4》map 、delete

    八、默认参数

    ES5:

    function point(x, y, isFlag){
      x = x || 0;
      y = y || -1;
      isFlag = isFlag || true;
      console.log(x,y, isFlag);
    }
    point(0, 0) // 0 -1 true 
    point(0, 0, false) // 0 -1 true 
    point(1) // 1 -1 true
    point() // 0 -1 true

    注意到了这里有问题,这里的默认参数先进行了布尔值的类型转换,因为undefined、0、false、null都是假。修改一下可以这样写

    function point(x, y, isFlag){
      x = x || 0;
      y = typeof(y) === 'undefined' ? -1 : y;
      isFlag = typeof(isFlag) === 'undefined' ? true : isFlag;
      console.log(x,y, isFlag);
    }
    point(0, 0) // 0 0 true
    point(0, 0, false) // 0 0 false
    point(1) // 1 -1 true
    point() // 0 -1 true
    

      

    ES6:

    function point(x = 0, y = -1, isFlag = true){
      console.log(x,y, isFlag);
    }
    point(0, 0) // 0 0 true
    point(0, 0, false) // 0 0 false
    point(1) // 1 -1 true
    point() // 0 -1 true

    转载请注明‘转载于Jason齐齐的博客http://www.cnblogs.com/jasonwang2y60/’

  • 相关阅读:
    Argparse4j
    Socket通信中的 BeginReceive与EndReceive
    socket 异步通信的一些问题
    static 还是readonly 还是static readonly
    隐藏Android下的虚拟按键
    IIS上架设https网站证书处理备忘
    [转] 移动平台Html5的viewport使用经验
    Air File.load加载问题
    x64位windows 2003 server中“Server 对象 错误 'ASP 0177 : 800700c1' Server.CreateObject 失败”问题
    报 "错误: 无法取消引用int" 的问题解决纪录
  • 原文地址:https://www.cnblogs.com/jasonwang2y60/p/6107811.html
Copyright © 2011-2022 走看看