• 继承


    构造函数、原型和实例的关系:每个构造函数都有一个原型对象 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继承

    待补充

    未完待续...

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

  • 相关阅读:
    Uri编码,包括javascript前端与C#服务器端
    快速排序
    bootstrap
    boosting
    bagging
    SHELL排序
    冒泡排序
    插入排序
    选择排序
    二叉树的数学性质
  • 原文地址:https://www.cnblogs.com/zhenjianyu/p/13406957.html
Copyright © 2020-2023  润新知