Appearance
类
class 关键字为语法糖写法,本质是原型链写法。
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
sayName() {
console.log(this.name);
}
sayAge() {
console.log(this.age);
}
}
const p = new Person('John', 18);
p.sayName();
p.sayAge();John
18function People(name, age) {
this.name = name;
this.age = age;
}
People.prototype.sayName = function () {
console.log(this.name);
};
People.prototype.sayAge = function () {
console.log(this.age);
};
const q = new People('John', 18);
q.sayName();
q.sayAge();John
18