Set
- ES6提供了新的数据结构set(集合),它类似于数组,但是成员的值都是唯一的
- set实现了interator接口,所以可以使用扩展运算符和for...of... 进行遍历
| 属性/方法 | |
|---|
| 声明set | let 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 |
| 清空set | s.clear(); |
| 遍历set | for(let v of s) |
Map
- ES6提供了Map数据结构,它类似于对象,也是键值对的集合。
- 但是‘键’的范围不仅局限于字符串,各种类型的值(包括对象)都可以当作键 (相当于升级版对象)
- Map也实现了interator接口,所以可以使用扩展运算符和for...of... 进行遍历
| 属性/方法 | |
|---|
| 创建Map | let 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'); |
| 清空map | m.clear(); |
| 获取键值 | m.get('key'); |
| 遍历map | for(let v of m) |
| 检测map某个元素是否存在 | console.log(m.has('key‘));返回true/false |
数组
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定义静态方法和属性 |
| 父类方法可以重写 |
- class声明类
class phone{
constructor(brand, price){
this.brand = brand;
this.price = price;
}
call(){
console.log("i can call");
}
}
let iphone = new phone('iphone', 6888);
- class静态成员
class phone{
static name = 'apple';
static change90{
console.log('i can change');
}
}
let nokia = new phone();
console.log(nokia.name);
console.log(phone.name);
- 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();
xiaomi.photo();
- 子类对父类重写
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();
- class中的getter和setter
class phone{
get price(){
console.log("价格属性被读取了");
return ‘iloveyou’;
}
set price(newVal){
console.log("价格属性被修改了");
}
}
let nokia = new phone();
console.log(nokia.price);
nokia.price = ‘free’;