es6

2018-09-29  本文已影响0人  隔壁老王z
class Person{
    constructor
}
class Worker extends Person{
    constructor(name,age,job){
        //super 超类(父类)
        super(name,age);
        this.job = job;
    }
showJob(){
        alert()
    }
}
const add = (a, b) => a + b;
const anotherAdd = (a, b) => { return a + b; };
function show(a,...args){
    console.log(a,args)
}
show(1,2,3)

//三个点的第一个用途,用来接收剩余 参数
//第二个用途,用来展开一个数组

//简写1
let a=12;
    let b=5;

    let json={a, b};

    console.log(json);
//简写2
let json={
      a: 12,
      b: 5,
      show(){
        alert(this.a+this.b);
      }
    };

    json.show();
let json={name: 'blue', age: 18};
  //alert('我叫:'+json.name+',我今年'+json.age+'岁');
  alert(`我叫:${json.name},我今年${json.age}岁`);
//原样输出
alert(`ab
cde
f
d`);
//startsWith和endsWith
if(num.startsWith('135')){
    alert('移动')
    }else{
    alert('联通')
}
if(fileName.endsWith('.txt')){
    alert('文本文件')
}else{
    alert('图片文件')
}
class Person{
      constructor(name, age){
        this.name=name;
        this.age=age;
      }

      showName(){
        alert('我叫'+this.name);
      }
      showAge(){
        alert('我'+this.age+'岁');
      }
    }

//```super```把属性带到子类,```extends```把方法代发哦子类
class Worker extends Person{
      constructor(name, age, job){
        //super-超类(父类)
        super(name, age);
        this.job=job;
      }

      showJob(){
        alert('我是做:'+this.job);
      }
    }

    let w=new Worker('blue', 18, '打杂的');

    w.showName();
    w.showAge();
    w.showJob();
var a=12;
    function show(){
      // 预解析时这里会有var a
      alert(a);
      var a=5;
    }
    show(); //undefined
//p1,p2,p3都执行了才会执行```then```
Promise.all([p1,p2,p3]).then()
Promise.all()   //所有都成功
Promise.race()   //只要有一个完成
- 回调和轮询

轮询好比每隔一段时间上前台查看快递,回调就好像快递到了前台来通知。
Promise有用-解除异步操作
Promise有局限 -带逻辑的操作麻烦

    function sleep(sec){
      return new Promise((resolve, reject)=>{
        setTimeout(function (){
          resolve();
        }, sec*1000);
      })
    }

    async function show(){
      alert('a');
      await sleep(1);
      alert('b');
      await sleep(2);
      alert('c');
    }

    show();
 async function(){
      ...
      ...
      let 结果 = await 异步操作(promise、generator、或另一个astnc函数)
      ...
    }
Array.of(3, 11, 8) // [3,11,8]
Array.of(3) // [3]
Array.of(3).length // 1

这个方法的主要目的,是弥补数组构造函数Array()的不足。因为参数个数的不同,会导致Array()的行为有差异。

Array() // []
Array(3) // [, , ,]
Array(3, 11, 8) // [3, 11, 8]

Array.of方法可以用下面的代码模拟实现。

function ArrayOf(){
  return [].slice.call(arguments);
}

延伸:Array.prototype.slice的一个重要应用就是将类似数组的对象转化为真正的数组。例如:

Array.prototype.slice.call({ 0: 'a', 1: 'b', length: 2 })
// ['a', 'b']
Array.prototype.slice.call(document.querySelectorAll("div"));
Array.prototype.slice.call(arguments);
上一篇 下一篇

猜你喜欢

热点阅读