一、类的定义
class Parent{ constructor(name="mukewang"){ this.name=name; } } let v_parant=new Parent("v"); console.log("constructor",v_parant);
类的继承
// extends 用于继承父类
//继承 class Parent{ constructor(name="mukewang"){ this.name=name; } } class Child extends Parent{ } console.log("extend", new Child()); //继承传递参数 //super必须放在设置改变参数的前面 { class Parent{ constructor(name="mukewang"){ this.name=name; } } class Child extends Parent{ constructor(name="child"){ super(name); this.type="child" } } console.log("extend", new Child("hello")); }
get 、set 方法
//getter,setter class Parent{ constructor(name="mukewang"){ this.name=name; } get longName(){ return "mk"+this.name } set longName(value){ this.name=value; } } let v=new Parent(); console.log("getter",v.longName); v.longName="hello" console.log("getter",v.longName);
静态方法
//静态方法 class Parent{ constructor(name="numewang"){ this.name=name; } static tell(){ console.log("tell"); } } Parent.tell(); //静态属性 class Parent{ constructor(name="numewang"){ this.name=name; } static tell(){ console.log("tell"); } } Parent.type="test"; console.log("静态属性",Parent.type); }