// 之前要抽象一个公共类的写法
function Student(name) {
this.name = name;
}
Student.prototype.hello = function () {
alert('Hello, ' + this.name + '!');
}
// class的写法 写一个公共类
class Student {
constructor(name) {
this.name = name;
}
hello() {
alert('Hello, ' + this.name + '!');
}
}
// new 一个实例的用法
var xiaoming = new Student('xiaoming');
// 继承的写法
class Child extends Student {
constructor (name, age) {
super(name);
this.age = age;
}
sayAge() {
console.log(`my age is${this.age}`);
}
}
其实 class 就是 之前 往原型上 添加hello 方法的一个语法糖(一种简写方式,达到的效果一样,名叫语法糖。)。