《大话设计模式》第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;
}

相关文章:

  • 2021-11-06
  • 2021-09-09
  • 2022-02-18
  • 2021-08-16
  • 2021-12-17
  • 2021-09-15
  • 2021-07-05
  • 2021-08-06
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-03-03
  • 2022-03-07
  • 2021-10-08
  • 2022-03-02
  • 2022-02-15
相关资源
相似解决方案