class是es6新增的一种语法糖,用于简化js中构造类的过程
1.es5中如何构造类?
function Person(name,age){this.name = name;this.age = age;
}
Person.prototype.sayName = function(){return this.name;
}
let p1 = new Person('小明',22);
2.es6中的class方式
class Person2{constructor(name,age){//实例化时会被立即调用this.name = name;this.age = age;}sayName(){return this.name;}sayAge(){return this.age;}
}
let p2 = new Person2('小乔',27)