zoukankan      html  css  js  c++  java
  • 继承

    构造函数、原型和实例的关系:每个构造函数都有一个原型对象 prototype ,原型对象都包含一个指向构造函数的指针 constructor ,而实例都包含一个指向原型对象的内部指针 __proto__ (读作:双下划线proto双下划线)。

    原型链继承

    function SuperType(){
        this.property = true
    }
    SuperType.prototype.getSuperValue = function(){
        return this.property
    }
    
    function SubType(){
        this.subproperty = false
    }
    SubType.prototype = new SuperType()
    SubType.prototype.getSubValue = function(){
        return this.subproperty
    }
    

    借用构造函数(伪造对象或经典继承)

    function SuperType(){
        this.colors = ['red','blue','green']
    }
    function SubType(){
        SuperType.call(this)
    }
    

    组合继承(伪经典继承)

    function Parent(name){
        this.name = name;
        this.colors = ['red', 'blue', 'green'];
    }
    Parent.prototype.getName = function(){
        console.log(this.name);
    }
    function Child(name,age){
        Parent.call(this,name);// 第二次调用 Parent()
        this.age = age;
    }
    Child.prototype = new Parent(); // 第一次调用 Parent()
    Child.prototype.constructor = Child;

    原型式继承

    function SuperType(name){
        this.name = name
    }
    
    function content(obj){
        function F(){}
        F.prototype = obj
        return new F
    }
    
    let superType = new SuperType()
    let sub = content(superType)

    寄生式继承

    function SuperType(name){
        this.name = name
    }
    
    function content(obj){
        function F(){}
        F.prototype = obj
        return new F
    }
    
    let superType = new SuperType()
    
    function subObject(obj){
        let sub = content(obj)
        sub.age = 18
        return sub
    }
    
    let sub = subObject(superType)

    寄生组合式继承

    function SuperType(name){
        this.name = name
    }
    function content(obj){
        function F(){}
        F.prototype = obj
        return new F
    }
    let con = content(SuperType.prototype)
    function SubType(){
        SuperType.call(this)
    }
    SubType.prototype = con
    con.constructor = SubType
    let sub = new SubType()

    es6继承

    待补充

    未完待续...

    (补充代码及各种继承方式出现的问题)

  • 相关阅读:
    qt 数据库操作总结
    Pro*C编程研究一:从.pc到.exe
    通过OCCI连接oracle(C++)
    数据库操作通用类
    一个数据库操作类,适用于Oracle,ACCESS,SQLSERVER
    如何卸载oracle11g
    ORA-12541:TNS:无监听程序
    Qt 线程基础(QThread、QtConcurrent等) 2
    QThreadPool类和QtConcurrent命名空间
    AE 向已存在的要素类中添加字段
  • 原文地址:https://www.cnblogs.com/zhenjianyu/p/13406957.html
Copyright © 2011-2022 走看看