es6中class實現面向對象編程的新形式
class Animal{
constructor(name,age){
this.name=name;
this.age=age;
}
static info="白色的";
say(){
console.log("汪汪~")
}
static show(){
console.log("yellow body")
}
};
const dog=new Animal("大黃",3)
注意:
- 1、在class的{}區間內,只能寫構造器,靜態方法,靜態屬性,實例方法;
- 2、class關鍵字,還是用原來的普通構造函數,把class關鍵字稱作語法糖
構造函數創建對象
fucntion Person(name,age){
this.name=name;
this.age=age;
}
const student=new Person("tinger",12)
Person.prototype.say=function(){
console.log("hello")
}
Person.show=function(){
console.log("long hair");
}