简体   繁体   English

展开三元语句或切换到if-else

[英]Expand a ternary statement or switch to if-else

I have the following which works fine with two conditions: 我有以下两个条件可以正常工作:

var value = this.value === "foo" ? function(d) {
    return d.foo;
} : function(d) {
    return d.bar;
};

What am I doing wrong when I try to expand the above to the following using if-else ? 当我尝试使用if-else将以上内容扩展为以下内容时,我在做什么错?

var value = 
if (this.value === "foo") {
        function(d) {
            return d.foo;
        };
    } else
if (this.value === "bar") {
    function(d) {
        return d.bar;
    };
} else {
    function(d) {
        return d.qux;
    };
}

I'm happy to stick with operators or switching to if-else : whatever is more readable for me. 我很乐意坚持使用运算符或切换到if-else:对我而言更易读的内容。

if cannot be used on the right-hand side of an assignment statement (which is part of why we have the conditional operator). if不能在赋值语句的右侧使用(这就是我们使用条件运算符的原因之一)。

Instead, assign in each branch: 而是在每个分支中分配:

var value;
if (this.value === "foo") {
    value = function(d) {
        return d.foo;
    };
} else if (this.value === "bar") {
    value = function(d) {
        return d.bar;
    };
} else {
    value = function(d) {
        return d.qux;
    };
}

Although note that that particular code can be simplified: 尽管注意可以简化特定代码:

var key = this.value == "foo" || this.value == "bar" ? this.value : "qux";
var value = function(d) {
    return d[key];
};

You can't use a conditional block statement to assign a value to a variable. 您不能使用条件块语句为变量分配值。 You can use the ternary operator you were using in the first example and double it: 您可以使用第一个示例中使用的三元运算符并将其加倍:

var value = (this.value === "foo") ? function(d) {
    return d.foo;
} : (this.value === "bar") ? function(d) {
    return d.bar;
} : function(d) {
    return d.qux;
};

The first once works because the ternary operator by default returns the result of either first or second expression based on the condition 第一个曾经有效,因为默认情况下三元运算符会根据条件返回第一个或第二个表达式的结果

https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Conditional_Operator https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Conditional_Operator

while the if-else block only check for the condition and do not return anything by default and thats why can not be on the right side of any assignment. 而if-else块仅检查条件,默认情况下不返回任何内容,这就是为什么不能在任何赋值的右侧。

There's another approach. 还有另一种方法。 It doesn't quite fit these requirements, but I do assume that these requirements aren't your precise ones (or do you have actual needs for 'foo', 'bar', and 'quz' :-) ? 它不完全符合这些要求,但是我确实假设这些要求不是您的确切要求(或者您是否真正需要'foo','bar'和'quz':-)? )

You can capture the value in a wrapping function: 您可以在包装函数中捕获值:

function createValueFunc(val) {
    return function(d) {
        return d[val];
    };
}

value = createValueFunc(this.value);

This does not handle your else case, so it may be useless to you. 这不能处理您的else情况,因此对您可能没有用。 But if it's useful, you can simplify it in ES6 to: 但是,如果有用,则可以在ES6中将其简化为:

createValueFunc = (name) => (obj) => obj[name];

You'll find such a function in many utility libraries. 您会在许多实用程序库中找到这种功能。 Underscore calls it property . 下划线称它为property Ramda calls it prop . 拉姆达称它为prop Sometimes I've seen it called get as well. 有时我也看到它叫做get

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM