Javascript运算符&&替代选择(限制所需)

时间:2012-12-08 16:45:06

标签: javascript

如何在没有&&的情况下编写以下内容?

if(a == 1 && b == 2) { ... }

我可以为操作员创建一个功能吗?

6 个答案:

答案 0 :(得分:4)

你可以这样做

if(a==1){
  if(b==2){
    JS function
  }
}

两者都是一样的,但if(a==1 && b==2)是一个很好的方法,可以完全相同。

答案 1 :(得分:4)

创建一个封装操作的函数:

function compare(a, b, value1, value2) {
    if(a === value1) {
        if(b === value2) {
            return true;
        }
    }
    return false;
}

你可以像这样使用它:

if(compare(a, b, 1, 2)) {
    // Your action..
}

答案 2 :(得分:1)

不确定为什么要这样做,但你可以嵌套if语句:

if(a == 1){
    if(b == 2){
        ...
    }
}

或者您可以使用按位运算符,如果您真的需要考虑21

if(b >> a === 1){
    ...
}

有很多方法可以做到,但这实际上取决于您的数据。

答案 3 :(得分:1)

您可以利用原型继承,并使用方法扩展其原型来构建构造函数。

function Comparer(a, b) {
    if (!(this instanceof Comparer))
        return new Comparer(a, b);

    this.assign(a, b);
    this.compare();
}

Comparer.prototype.result = false;
Comparer.prototype.compare = function() {
    this.result = this.a == this.b;
};
Comparer.prototype.assign = function(a, b) {
    this.a = a;
    this.b = b;
};
Comparer.prototype.and = function(a, b) {
    this.assign(a, b);
    if (this.result !== false) 
        this.compare();
    return this;
};
Comparer.prototype.or = function(a, b) {
    this.assign(a, b);
    if (this.result !== true) 
        this.compare();
    return this;
};

并像这样使用它:

var a = 1,
    b = 2;

if (Comparer(a, 1).and(b, 2).result) 
    console.log("pass");
else
    console.log("fail");

我们甚至可以扩展它以摆脱if语句。

Comparer.prototype.then = function(fn) {
    if (this.result === true)
        fn();
    return this;
};
Comparer.prototype.otherwise = function(fn) {
    if (this.result === false)
        fn();
    return this;
};

并像这样使用它:

var a = 1,
    b = 2;

Comparer(a, 1)
    .and(b, 2)
    .then(function() { console.log("pass"); })
    .otherwise(function() { console.log("fail"); });

或者缩短这样的事情:

var log = Function.bind.bind(console.log, console);

var a = 1,
    b = 2;

Comparer(a, 1)
    .and(b, 2)
    .then(log("pass"))
    .otherwise(log("fail"));

答案 4 :(得分:0)

如果你想将运算符&&实现为函数,它会变得丑陋,因为你需要将条件作为闭包传递,无论何时你关心短路副作用。

示例

if(condition && changeTheWorld()) { ... }

// Cannot be translated into a function call of this nature:
if(land(condition, changeTheWorld()) { ... }

相反,您需要创建一个闭包:

if(land(condition, function() {return changeTheWorld()}) {...}

正如你所看到的,虽然没有优势,但它真的很麻烦和冗长。

如果你真的需要这个作为一个功能,这是一个

短路实施

这个函数正确地模拟了&&的语义,如果你传递了那些必须不被评估的条件 - 在短路的情况下 - 作为函数而不是表达式。

换句话说,函数按顺序遍历参数,如果一个是函数,它首先计算它,否则它只取值,如果是假的,它会通过返回false而中止,否则,它会继续下一个参数。

function land(){
    for(var i = 0; i < arguments.length; i++) {
        var operand = arguments[i];
        var value = (typeof operand === 'function') ? operand() : operand;
        if (!value) {
            return false;
        }
    }
    return true;
}

示例

function evaluateTo(result) {
    return function() {
            console.log("Evaluating " + result);
        return result;
    };
}

if(land(true, evaluateTo(1))) {
    console.log("All truthy");
}
// Outputs:
// Evaluating 1
// All truthy

if(land(evaluateTo(1), evaluateTo(0), evaluateTo(true))) {
    console.log("All truthy");
}
// Outputs:
// Evaluating 1
// Evaluating 0

强制性导弹示例

function changeTheWorld() {
    console.log("Missiles away!");
    // Firing 3 missiles
    return nukeTheGlobe(3);
}

if(false && changeTheWorld() == 3) { ... }
// we survived, missiles not fired

if(naiveLand(maybe, changeTheWorld() == 3) { ... }
// Missiles away! no matter what value `maybe` has

if(land(false, function(){ return changeTheWorld() == 3; })) {...}
// we survived, missiles not fired

答案 5 :(得分:0)

您的问题毫无意义,但您可以使用乘法运算符*代替&&

if(a==1 * b==2){
    //do something
}