//通过function来定义类functionPerson1(name,age){this.age = age
this.name = name
}Person1.prototype.running=function(){}Person1.prototype.eating=function(){}var p1 =newPerson1("大余",25)
console.log(p1.__proto__ ===Person1.prototype);//true//class来定义类,这种写法只是上面的语法糖classPerson2{constructor(name,age){this.name = name
this.age = age
}running(){}eating(){}}var p2 =newPerson2("小余",20)
console.log(p2.__proto__ ===Person2.prototype);//true
console.log(Person2.prototype.constructor);//指回类本身,跟上面Person1是一样的
console.log(typeof Person1,typeof Person2);//都是function
class 与 function 的不同点
//function的写法是能够作为普通函数去调用的functionPerson1(name,age){//这两个需要写,如果不写,name,age打印不出来。如果你name能够打印出来的话,那是你之前有用过,他存到window里面去了,因为Person1函数的外面一层就是window,这个时候就算你把Person1("大满",24)的大满修改掉,控制台也是无法修改掉的,因为函数内部的this.name是去window中读取了this.name = name
this.age = age
console.log(this.name+"今年已经"+this.age);}Person1("大满",24)//大满今年已经24//class定义的类,不能作为一个普通的函数进行调用//class的写法则是不行,结果就是报错。不使用 new 的时候是不能够调用的classPerson2{constructor(name,age){this.name = name
this.age = age
}}Person2("小余",20)//Class constructor Person2 cannot be invoked without 'new'