美文网首页
对象的继承实现

对象的继承实现

作者: dwy_interesting | 来源:发表于2018-11-06 13:50 被阅读0次

    继承实现:
    a. 构造函数继承
    call()
    apply()
    b. 原型链继承
    Student.prototype = new Person();

    Student.prototype = Object.create(Person.prototype);
    c. 组合继承
    构造函数 + 原型链继承
    d. class
    语法糖,本质上还是ES5基于原型的继承:构造函数 + prototype
    语法:
    class 类名 {
    constructor() {
    // 构造函数,constructor 是构造函数的固定名称
    }

                methodName1() { 
                                  // 成员方法,相当于就是在 prototype 中定义的方法
                }
    
                methodName2() {
                                  // 成员方法,相当于就是在 prototype 中定义的方法
                }
            }
        继承:
            class 子类名 extends 父类名(超类、基类) {
                constructor() { 
                                  // 构造函数,constructor 是构造函数的固定名称
                    super(); // 调用父类的构造函数
                }
    
                methodName1() {
                                  // 成员方法,相当于就是在 prototype 中定义的方法
                    super.methodName(); // 调用父类的成员函数
                }
    
                methodName2() {
                                  // 成员方法,相当于就是在 prototype 中定义的方法
                }
            }
    e. 寄生组合方式
    f. 拷贝式继承

    相关文章

      网友评论

          本文标题:对象的继承实现

          本文链接:https://www.haomeiwen.com/subject/ljssaftx.html