《大话设计模式》第1章:用JavaScript语言描述(三)

《大话设计模式》第1章:用JavaScript语言描述(三)第一章1.9节的源代码用JS的原型继承重写:

function Operation() {

}

Operation.prototype.numA = 0;

Operation.prototype.numB = 0;

Operation.prototype.GetResult = function() {

    var result = 0.00;

    return result;

}


function OperationAdd() {

}

OperationAdd.prototype = new Operation();

OperationAdd.prototype.GetResult = function() {

    var result = 0.00;

    result = numA + numB;

    return result;

}

function OperationSub() {

}

OperationSub.prototype = new Operation();

OperationSub.prototype.GetResult = function() {

    var result = 0.00;

    result = numA - numB;

    return result;

}

function OperationMul() {

}

OperationMul.prototype = new Operation();

OperationMul.prototype.GetResult = function() {

    var result = 0.00;

    result = numA * numB;

    return result;

}

function OperationDiv() {

}

OperationDiv.prototype = new Operation();

OperationDiv.prototype.GetResult = function() {

    var result = 0.00;

    if(numB == 0)

    {

        alert('除数不能为0。');

        return false;        

    }

    result = numA / numB;

    return result;

}

你可能感兴趣的:(JavaScript)