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一些函数
    js时间字符串转时间戳
    golang学习之interface与其它类型转换
    golang学习之奇葩的time format
    windows下安装mongodb
    golang学习之struct
    golang学习之闭包
    js生成6位随机码
    golang学习之生成代码文档
    moment常用操作
  • 原文地址:https://www.cnblogs.com/zhenjianyu/p/13406957.html
Copyright © 2011-2022 走看看