ES6之后类的底层逻辑
一、构造函数(Constructor)部分
class Person { constructor(name, age) { this.name = name; this.age = age; } }
二、原型(Prototype)部分
三、继承的实现
Last updated
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
}Last updated
class Person {
constructor(name, age) {
this.name = name;
this.age = age;
}
sayHello() {
console.log(`Hello, my name is ${this.name} and I am ${this.age} years old.`);
}
}class Student extends Person {
constructor(name, age, grade) {
super(name, age);
this.grade = grade;
}
study() {
console.log(`${this.name} is studying.`);
}
}