原型模式(prototype)是指用原型实例指向创建对象的种类,并且通过拷贝这些原型创建新的对象。

    1. class Person {
    2. constructor(name) {
    3. this.name = name
    4. }
    5. getName() {
    6. return this.name
    7. }
    8. }
    9. class Student extends Person {
    10. constructor(name) {
    11. super(name)
    12. }
    13. sayHello() {
    14. console.log(`Hello My name is ${this.name}`)
    15. }
    16. }
    17. let student = new Student("xiaoming")
    18. student.sayHello()

    原型模式,就是创建一个共享的原型,通过拷贝这个原型来创建新的类,用于创建重复的对象,带来性能上的提升。