Set、Map、Class和数组

138 阅读3分钟

Set

  • ES6提供了新的数据结构set(集合),它类似于数组,但是成员的值都是唯一
  • set实现了interator接口,所以可以使用扩展运算符for...of... 进行遍历
属性/方法
声明setlet s = new Set(); let s = new Set(['apple', 'red', 'round']);
查看set个数s.size
添加新的元素s.add('sweet');
删除新的元素s.delete('sweet');
检测元素是否存在console.log(s.has('apple')); 返回true/false
清空sets.clear();
遍历setfor(let v of s)

Map

  • ES6提供了Map数据结构,它类似于对象,也是键值对的集合
  • 但是‘键’的范围不仅局限于字符串,各种类型的值(包括对象)都可以当作键 (相当于升级版对象)
  • Map也实现了interator接口,所以可以使用扩展运算符for...of... 进行遍历
属性/方法
创建Maplet map = new Map(); let s = new Map(['apple', 'red', 'round']);
查看Map个数m.size
添加新的元素m.set('name','lena'); let key = { school:'HKU' };``m.set(key, ['bj','sh','hk']);
删除新的元素m.delete('name');
清空mapm.clear();
获取键值m.get('key');
遍历mapfor(let v of m)
检测map某个元素是否存在console.log(m.has('key‘));返回true/false

数组

  • 数组去重
// 1. set去重
let arr = [1,2,3,3,3,4,5,4];
let result = [...new Set(arr)];
console.log(result);
  • 求两个数组的交集
let arr = [1,2,3,3,3,4,5,4];
let arr2 = [2,3,3,3,4,5,6,7];
let result = [...new set(arr)].filter(item => new Set(arr2).has(item));
console.log(result);
  • 求两个数组的并集
let arr = [1,2,3,3,3,4,5,4];
let arr2 = [2,3,3,3,4,5,6,7];
let result = [...new Set([...arr,...arr2])];
console.log(result);
  • 求两个数组的差集
let arr = [1,2,3,3,3,4,5,4];
let arr2 = [2,3,3,3,4,5,6,7];
let result = [...new set(arr)].filter(item => !(new Set(arr2).has(item)));
console.log(result);

Class

  • 通过class关键字,可以定义类
  • class是es6的语法糖,它的绝大多数功能es5也能做到,新的class写法只是让对象原型的写法更加清晰、更像面向对象编程的语法而已
知识点
class声明类
constructor定义构造函数初始化
extends继承父类
super调用父级构造方法
static定义静态方法和属性
父类方法可以重写
  1. class声明类
//定义类
class phone{
  //构造函数不是必须的,没有也可以
  constructor(brand, price){
     this.brand = brand;
     this.price = price;
  }
  
  call(){
      console.log("i can call");
  }
}

//实例化
let iphone = new phone('iphone', 6888);
  1. class静态成员
class phone{
  static name = 'apple';
  static change90{
    console.log('i can change');
  }
}

let nokia = new phone();
console.log(nokia.name);//undifined
console.log(phone.name);//apple 属性属于类,不属于实例对象
  1. class类继承
//定义类
class phone{
  
  constructor(brand, price){
     this.brand = brand;
     this.price = price;
  }
  
  call(){
      console.log("i can call");
  }
}

class Smartphone extends phone{
  
  constructor(brand, price, color, size){
    super(brand, pirce);
     this.color = color;
     this.size = size;
  }
  
  photo(){
      console.log("i can photo");
  }
  
   palyGame(){
      console.log("i can palyGame");
  }
}

const xiaomi = new Smartphone("xiaomi", "4555", "red", "5")
xiaomi.call();//i can call
xiaomi.photo();//i can photo
  1. 子类对父类重写
//定义类
class phone{
  
  constructor(brand, price){
     this.brand = brand;
     this.price = price;
  }
  //重写
  call(){
      console.log("i can call");
  }
}

class Smartphone extends phone{
  
  constructor(brand, price, color, size){
    super(brand, pirce);
     this.color = color;
     this.size = size;
  }
  
  call(){
      console.log("i can call a long time");
  }

}

const xiaomi = new Smartphone("xiaomi", "4555", "red", "5")
xiaomi.call();//i can call a long time
  1. class中的getter和setter
class phone{
  get price(){
    console.log("价格属性被读取了");
    return ‘iloveyou’;
  }
  
  set price(newVal){
    console.log("价格属性被修改了");
  }
}

let nokia = new phone();
console.log(nokia.price);//价格属性被读取了 iloveyou
nokia.price = ‘free’;//价格属性被修改了