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");
}