Class: 对一类具有共同特征的事物的抽象(构造函数语法糖)。

constructor() 基本定义和生成实例

 class Parent {
     constructor(name = 'es6'){
         this.name = name
     }
 }
 let data = new Parent('yummy')
 console.log(data) // Parent { name: 'yummy} 

extends 继承

 class Parent {
     constructor(name = 'es6'){
         this.name = name
     }
 }
 
 // 普通继承
 class Child extends Parent {}
 console.log(new Child()) //  Child { name: 'es6'} 
 
 // 传递参数
 class Child extends Parent {
    constructor(name = "child") {
        super(name);
        this.type = "child";
    }
 }
 console.log(new Child('yummy')) //  Child { name: 'yummy, type: 'child'}

getter setter

  • 这个两个方法比较重要,常常用来封装API
  • get 和 set 是属性,而不是方法
 class Parent {
     constructor(name = 'es6'){
         this.name = name
     }
     // getter
     get getName() {
         return 'sy' + this.name
     } 
     // setter
     set setName(value){
         this.name = value
     }
 }
 
 let data = new Parent()
 console.log(data.getName) // syes6
 
 data.setName = 'yummy'
 console.log(data.getName) // yummy

static 静态方法

  • 静态方法,不能在类的实例上调用静态方法,而应该通过类本身调用
 class Parent {
     static getName = (name) => {
         return `你好!${name}`
     }
 }
 
 console.log(Parent.getName('yummy')) // 你好!yummy

静态属性

 class Parent {}
 Parent.type = "test";
 
 console.log(Parent.type); //test

基本数据类型

在ES6中一共有7种,分别是:srtingnumberbooleanobjectnullundefinedsymbol

其中 object 包含: ArrayFunctionDateRegExp

而在ES11后新增一中,为 8种 分别是:srtingnumberbooleanobjectnullundefinedsymbolBigInt

posted on 2023-01-02 21:34  羽丫头不乖  阅读(30)  评论(0编辑  收藏  举报