在傳統(tǒng)的JS中定義對(duì)象的方法:
function Person(name,age){
this.name=name;
this.age=age;
}
Person.prototype.sayName=function(){
console.log(this.name);
}
在ES6中定義對(duì)象的方法:
class Person{
constructor(name,age){
this.name=name;
this.age=age;
}
sayName(){
console.log(this.name);
}
}