1. 程式人生 > >ES6類和物件

ES6類和物件

class Parent{
constructor(name='hkj'){
this.name=name;
}
static fun(){ //定義靜態方法(類可直接呼叫,不需要生成物件)
alert("this is static function");
} }
Parent.type="parent type";//靜態屬性
Parent.fun(); //呼叫靜態方法
alert(Parent.type);//呼叫靜態屬性
class Child extends Parent{//繼承父類
constructor(name="hukai"){
super(name); //子類建構函式第一行必須顯示呼叫父類建構函式
this.type="child";
}
set setName(name){//set屬性!!!不是方法
this.name=name;
}
get getName(){//get屬性
return "childName:"+this.name;
}
}
let child=new Child();
console.log(child.name);
console.log(child.type);


let child1=new Child("hukaijing");

console.log(child1.name)

console.log(child1.type);