JS 继承机制一 对象冒充

/* 继承方式一: 对象冒充,构造函数只是一个函数,所以可以使ClassA的构造函数称为ClassB的方法,然后调用它。this指向的是所属的对象 */

    // 超类ClassA
    function ClassA(color){
        this.color = color;
        this.sayColor = function(){
            alert(this.color);
        }
    }
    // 超类ClassB
    function ClassB(age){
        this.age = age;
        this.sayAge = function(){
            alert(this.age);
        }
    }
    //对象继承可以实现多重继承
    function ClassC(color,name,age){
        //函数名只是指向它的指针
        this.newMethod = ClassA;
        this.newMethod(color);
        //delete删除了对ClassA的引用,这样以后就不能在调用它。
        delete this.newMethod;

        //多重继承需要注意的事,如果ClassA和ClassB具有同名的属性和方法,ClassB具有高优先级,因为继承的是最后的类。
        this.newMethod = ClassB;
        this.newMethod(age);
        delete this.newMethod;

        //所有的新属性和新方法都必须在删除了新方法的代码后定义,否者可能会覆盖掉超类的相关属性和方法。
        this.name = name;
        this.sayName = function(){
            alert(this.name);
        } 
    }

    var objA = new ClassA("black");
    var objB = new ClassB(26);
    var objC = new ClassC("yellow","pape",25);
    objA.sayColor(); //black
    objB.sayAge(); //26
    objC.sayColor(); //yellow
    objC.sayName(); //pape
    objC.sayAge(); //25
原文地址:https://www.cnblogs.com/xmyun/p/5923730.html