ES6-类-ES6新增的类的概念
// 使用 class 关键字定义 PersonClass 类
class PersonClass {
// PersonClass 类的构造函数,等价于构造函数 PersonType
// 当我们创建 PersonClass 的实例时,该方法会自动被调用。
constructor(fullName) {
this.fullName = fullName;
}
// 为原型对象添加的方法,等于下面的代码:
// Object.defineProperty(PersonType.prototype, 'sayName', {
// configurable: true,
// enumerable: false,
// writable: true,
// value: function () {
// return this.fullName;
// }
// })
//为构造函数的原型对象内添加方法
sayName() {
return this.fullName;
}
//为构造函数添加静态方法。构造函数本身的方法
static.sayAge() {
return '22';
}
}
let student1 = new PersonClass('刘旭凯');
console.log(student1)
// 通过与之前的构造函数对比,类和构造函数的作用是一样的。
// 其实类的语法只是一种语法糖,实质上等价于一个自定义类型的构造函数。