ES6-ES11新特性学习连载篇(6)

63 阅读1分钟

1、集合Set

  let s = new Set();//object对象
  let s1 = new Set(["1","2","3","1","2"]);
  //输出时候回自动去重
  //add 增加   delete  删除  has 检测 clear //清空
  for(let v of s1){
      console.log(v);//遍历
  }

2、Map

    let m = new Map();
    m.set('name','xiaoyu');//添加元素
    console.log(m.size)//size 1
    m.delete('name')//删除
    console.log(m.get('name'))//获取
    m.clear()// 清空
    for(let v of m){
        console.log(v);
    }
    

3、class

 class phone{
     constructor(brand){
         this.brand = brand;
     }
     call(){
         console.log('call');
     }
  } 
     let oneplus = new Phone('huawei');
     console.log(oneplus);
     
     
//静态成员
class Tel{
    static name = 'shouji';
    static change(){
        console.log('mine shouji');
    }    
}
let nokia = new Tel();
console.log(nokia.name);//undefined
 console.log(Tel.name);//shouji
 
 
 //类继承
 
  class Phone{
     constructor(brand){
         this.brand = brand;
     }
     call(){
         console.log('call');
     }
  } 
 class SmartPhone extends Phone{
     constructor(brand,color){
         super(brand);
         this.color = color;
     }
     photo(){
         console.log('photo');
     }  
  } 
     
let xiaomi = new SmartPhone('小米','黑色')

//get 和 set 方法
class Phone{
    get price(){
      console.log('读取');
      return 'i love you';//返回值就是属性值
    }
    set price(newval){
        console.log('修改');
    }
}
let s = new Phone();
console.log(s.price);
s.price = 'free';