构造函数与原型方法
// class 关键词// function Person (name) {// this.name = name// }// Person.prototype.say = function () {// console.log(`hi, my name is ${this.name}`)// }class Person { constructor (name) { this.name = name } say () { console.log(`hi, my name is ${this.name}`) }}const p = new Person('tom')p.say()
静态方法
// static 方法class Person { constructor (name) { this.name = name } say () { console.log(`hi, my name is ${this.name}`) } static create (name) { return new Person(name) }}const tom = Person.create('tom')tom.say()
继承
// extends 继承class Person { constructor (name) { this.name = name } say () { console.log(`hi, my name is ${this.name}`) }}class Student extends Person { constructor (name, number) { super(name) // 父类构造函数 this.number = number } hello () { super.say() // 调用父类成员 console.log(`my school number is ${this.number}`) }}const s = new Student('jack', '100')s.hello()