美文网首页
ES6解读3:类class

ES6解读3:类class

作者: 蚊小文 | 来源:发表于2017-10-12 09:40 被阅读0次

类的继承

    class Parent{
        constructor(){
            this.a=1;
            this.b=[1,2,this.a];
            this.show=()=>{
                console.log(this.a,this.b)
            }
        }
    }
    class Child extends Parent{
        constructor(){
            super();
            this.a=2;
        }
    }
    let child=new Child();
    child.a=11;
    //child.show();//11 [1,2,1]

类的getter和setter方法

{
class Parent{
  constructor(name='leilei'){
      this.name=name;
  }
  get longName(){
      return 'ymy '+this.name;
  }
  set longName(value){
      this.name=value;
  }
}

静态方法以及静态属性

class Parent{
  constructor(name='leilei'){
      this.name=name;
  }
  //设置静态方法
  static tell(){
      console.log('tell');
  }
}
Parent.sex='gril';  //设置类的静态属性
Parent.tell() //调用类的静态方法;

注意:静态方法只能是类调用,不能实例调用

相关文章

网友评论

      本文标题:ES6解读3:类class

      本文链接:https://www.haomeiwen.com/subject/wxyuyxtx.html