介绍
ES6 提供了更接近传统语言的写法,引入了 Class(类)这个概念,作为对象的模板。通过 class 关键字,可以定义类。基本上,ES6 的 class 可以看作只是一个语法糖,它的绝大部分功能,ES5 都可以做到,新的 class 写法只是让对象原型的写法更加清晰、更像面向对象编程的语法而已。
            
            
              js
              
              
            
          
          class shouji {
    constructor(brand,price) {
        this.brand=brand;
        this.price=price
    }
    call(){
        console.log('我可以打电话')
    }
}
let A = new shouji('1+',1999);
console.log(A)知识点:
- class 声明类
- constructor 定义构造函数初始化
- extends 继承父类
- super 调用父级构造方法
- static 定义静态方法和属性
- 父类方法可以重写
静态成员
            
            
              js
              
              
            
          
          class Person{
    static name='手机'
}
let nokia = new Person();
console.log(nokia.name);构造函数继承
            
            
              js
              
              
            
          
          function Phone(brand,price){
    this.brand=brand;
    this.price=price;
}
Phone.prototype.call=function (){
    console.log("我可以打电话");
}
function SmartPhone(brand,price,color,size){
    Phone.call(this,brand,price);
    this.color=color;
    this.size=size;
}
//设置子级构造函数原型
SmartPhone.prototype=new Phone;
SmartPhone.prototype.constructor=SmartPhone;
//声明子类方法
SmartPhone.prototype.photo = function (){
    console.log('我可以玩游戏');
}
const chuizi = new SmartPhone('锤子',2499,'黑色','5.5inch')
console.log(chuizi);Class 的类继承
            
            
              js
              
              
            
          
          class Phone{
    constructor(brand,price) {
        this.brand=brand;
        this.price=price;
    }
    //父类的成员属性
    call(){
        console.log('我可以打电话')
    }
}
class SmartPhone extends Phone{
    constructor(brand,price,color,size) {
        super(brand,price);
        this.color=color;
        this.size=size;
    }
    photo(){
        console.log('拍照');
    }
    playGame(){
        console.log('打游戏');
    }
}
const xiaomi=new SmartPhone('小米',1999,'黑色','4.7inch')
xiaomi.call();
xiaomi.photo();
xiaomi.playGame();子类对父类方法的重写
            
            
              js
              
              
            
          
          class Phone{
    constructor(brand,price) {
        this.brand=brand;
        this.price=price;
    }
    //父类的成员属性
    call(){
        console.log('我可以打电话')
    }
}
class SmartPhone extends Phone{
    constructor(brand,price,color,size) {
        super(brand,price);
        this.color=color;
        this.size=size;
    }
    photo(){
        console.log('拍照');
    }
    playGame(){
        console.log('打游戏');
    }
    //重写!
    call(){
        console.log('我可以进行视频通话')
    }
}
const xiaomi=new SmartPhone('小米',1999,'黑色','4.7inch')
xiaomi.call();
xiaomi.photo();
xiaomi.playGame();get 和 set 设置
            
            
              js
              
              
            
          
          class Phone{
    get price(){
        console.log("价格被读取了")
        return 'I LOVE YOU'
    }
    set price(val){
        console.log('价格被修改了')
        return val;
    }
}
//实例化对象
let s = new Phone();
s.price=12  
// console.log(s.price)   //其实是调用price方法