您的位置:首页 > Web前端 > JavaScript

基于JavaScript的公式解释器 - 9 【逻辑操作符的实现】

2010-10-18 22:52 423 查看
逻辑操作符的基类型

逻辑操作符包括一元和二元类型,因此有两个基类型

文件:OperatorLogicBase.js

function OperatorBinaryLogicBase() {
    this.DeriveFrom(new OperatorBinaryBase());
    this.set_ClassName("OperatorBinaryLogicBase");
    this.set_Sign("undefined");
};

function OperatorUnaryLogicBase() {
    this.DeriveFrom(new OperatorUnaryBase());
    this.set_ClassName("OperatorUnaryLogicBase");
    this.set_Sign("undefined");
};




逻辑操作符的实现

文件:OperatorLogic.js

function OperatorLogicAnd() {
    this.DeriveFrom(new OperatorBinaryLogicBase());
    this.set_ClassName("OperatorLogicAnd");
    this.set_Sign("&&");
};

OperatorLogicAnd.prototype.Evaluate = function(operands) {
    var values = this._getTwoNumberOperands(operands);

    return new OperandBoolean(values[0].get_Value() && values[1].get_Value());
};

function OperatorLogicOr() {
    this.DeriveFrom(new OperatorBinaryLogicBase());
    this.set_ClassName("OperatorLogicOr");
    this.set_Sign("||");
};

OperatorLogicOr.prototype.Evaluate = function(operands) {
    var values = this._getTwoNumberOperands(operands);

    return new OperandBoolean(values[0].get_Value() || values[1].get_Value());
};

function OperatorLogicNot() {
    this.DeriveFrom(new OperatorUnaryLogicBase());
    this.set_ClassName("OperatorLogicOr");
    this.set_Sign("!");
};

OperatorLogicNot.prototype.Evaluate = function(operands) {
    var val = this.GetOneNumberOperand(operands);

    return new OperandBoolean(!val.get_Value());
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: