JavaScript对象继承的实现

1.综合对象冒充(属性)、原型链继承(对象)方法:

function ClassA(sColor){
   this.color = sColor;
}

ClassA.prototype.sayColor = function(){
   alert(this.color);
};


function ClassB(sColor, sName){
   ClassA.call(this, sColor);  
   this.name = sName;


ClassB.prototype = new ClassA();
ClassB.prototype.sayName = function(){
   alert(this.name);
}

//测试:
var b = new ClassB("red", "sangKaNa");
b.sayColor();
b.sayName();

2.调用zInherit库继承:

引用zinherit.js文件,这里列出文件源码:

Object.prototype.inheritFrom = function (fnClass) {

    function inheritClasses(fnClass, arrClasses) {
        
        arrClasses.push(fnClass);

        if (typeof fnClass.__superclasses__ == "object") {
            for (var i=0; i < fnClass.__superclasses__.length; i++){
                inheritClasses(fnClass.__superclasses__[i], arrClasses);
            }
        }
    }
    
    if (typeof this.constructor.__superclasses__ == "undefined") {
        this.constructor.__superclasses__ = new Array();
    }
    
    inheritClasses(fnClass, this.constructor.__superclasses__);
    
    for (prop in fnClass.prototype) {
        if (typeof fnClass.prototype[prop] == "function") {
            this[prop] = fnClass.prototype[prop];
        }
    }
};

Object.prototype.instanceOf = function (func) {

    if (this.constructor == func) {
        return true;
    } else if (typeof this.constructor.__superclasses__ == "object") {
        for (var i=0; i < this.constructor.__superclasses__.length; i++) {
            if (this.constructor.__superclasses__[i] == func) {
                return true;
            }
        }
        return false;
    } else {
        return false;
    }
};

应用:

function ClassX() {
    this.messageX = "This is the X message.";

    if (typeof ClassX._initialized == "undefined") {
        
        ClassX.prototype.sayMessageX = function () {
            alert(this.messageX);
        };

        ClassX._initialized = true;
    }
}

function ClassY() {
    this.messageY = "This is the Y message.";

    if (typeof ClassY._initialized == "undefined") {
        
        ClassY.prototype.sayMessageY = function () {
            alert(this.messageY);
        };

        ClassY._initialized = true;
    }
}

function ClassZ() {
    ClassX.apply(this);
    ClassY.apply(this);
    this.messageZ = "This is the Z message.";

    if (typeof ClassZ._initialized == "undefined") {
        
        ClassZ.prototype.inheritFrom(ClassX);
        ClassZ.prototype.inheritFrom(ClassY);

        ClassZ.prototype.sayMessageZ = function () {
            alert(this.messageZ);
        };

        ClassZ._initialized = true;
    }
}

测试:

var objZ = new ClassZ();
objZ.sayMessageX();
objZ.sayMessageY();
objZ.sayMessageZ();

3.应用xbObjects库:

<script type="text/javascript" src="xbObjects.js"></script>
<script type="text/javascript">

_classes.registerClass("Polygon");

function Polygon(iSides) {
    
    _classes.defineClass("Polygon", prototypeFunction);

    this.init(iSides);
    
    function prototypeFunction() {
    
        Polygon.prototype.init = function(iSides) {
            this.parentMethod("init");
            this.sides = iSides;            
        };
    
        Polygon.prototype.getArea = function () {
            return 0;
        };    
    
    }
}

_classes.registerClass("Triangle", "Polygon");

function Triangle(iBase, iHeight) {

    _classes.defineClass("Triangle", prototypeFunction);
    
    this.init(iBase,iHeight);
    
    function prototypeFunction() {
        Triangle.prototype.init = function(iBase, iHeight) {
            this.parentMethod("init", 3);
            this.base = iBase;
            this.height = iHeight;
        };
        
        Triangle.prototype.getArea = function () {
            return 0.5 * this.base * this.height;
        };    
    }
    
}

_classes.registerClass("Rectangle", "Polygon");

function Rectangle(iLength, iWidth) {

    _classes.defineClass("Rectangle", prototypeFunction);
    
    this.init(iLength, iWidth);
    
    function prototypeFunction() {
        Rectangle.prototype.init = function(iLength, iWidth) {
            this.parentMethod("init", 4);
            this.length = iLength;
            this.width = iWidth;
        }
    
       Rectangle.prototype.getArea = function () {
            return this.length * this.width;
       };    
        
    }
}

var triangle = new Triangle(12, 4);
var rectangle = new Rectangle(22, 10);

alert(triangle.sides);
alert(triangle.getArea());

alert(rectangle.sides);
alert(rectangle.getArea());

</script>

说明:

1.zInherit库下载地址:http://www.nczonline.net/downloads

2.xbObjects库下载地址:http://archive.bclary.com/xbProjects-docs/xbObject/

原文地址:https://www.cnblogs.com/Langzi127/p/2151741.html