Javascript 速记 if-else 并返回
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9056112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript shorthand if-else and returning
提问by ntkachov
Can the javascript shorthand for if-else return out of a function? If so how would this work.
if-else 的 javascript 简写可以从函数中返回吗?如果是这样,这将如何工作。
eg. I have this:
例如。我有这个:
if(boolean){
return;
}
and I would like to write it as this:
我想写成这样:
(value)? return;
Chrome complains that return is unexpected. Is there anyway to write something like this so that it is valid?
Chrome 抱怨返回是意外的。反正有没有写这样的东西,以便它是有效的?
回答by Darin Dimitrov
No, you can't do that unless you return a value. For example if your function had to return a value you could have written:
不,除非你返回一个值,否则你不能这样做。例如,如果您的函数必须返回一个您可以编写的值:
return boolean ? 'foo' : 'bar';
But you cannot stop the execution of the function by returning void using the conditional operator.
但是您不能通过使用条件运算符返回 void 来停止函数的执行。
回答by Shiplu Mokaddim
If you intend to return from the function at this point in its execution regardlessof whether the test evaluates true or false, you can use,
如果您打算在执行过程中的此时从函数返回,而不管测试的结果是真还是假,您都可以使用,
return (value) ? 1 : 2;
But if you merely wish to return early when a test evaluates true (for instance, as a sanity-check to prevent execution when parameters are invalid), the shortest you can make it is:
但是,如果您只是希望在测试评估为真时尽早返回(例如,作为健全性检查以防止在参数无效时执行),那么您可以做到的最短时间是:
if (boolean) return;
回答by sapy
if(boolean) return;
Single line , readable , perfectly valid;
单行,可读,完全有效;
回答by Emeeus
I know it's an old question but I want to add that there is a non-standard way to return out of a function in shorthand if-else, and that is performing Immediately Invoked Function Expression (IIFE):
我知道这是一个老问题,但我想补充一点,有一种非标准的方法可以用 if-else 速记从函数中返回,那就是执行立即调用函数表达式(IIFE):
function outOfFunction(boolean){
return (boolean)?(()=>{return true;})():(()=>{return false;})();
}
console.log(outOfFunction(true));
console.log(outOfFunction(false));
And if we want to be out of the function or continue with other task:
如果我们想退出函数或继续其他任务:
function shorthandExampleJustTrue(boolean){
var someVar = "I'm out";
return (boolean)?(()=>{return true;})():(()=>{
console.log("here my code ");
console.log(someVar);
return "anythig else";
})();
}
console.log(shorthandExampleJustTrue(true));
console.log(shorthandExampleJustTrue(false));
When we use arrow functions we are able to access to variables out of the immediate function context.
当我们使用箭头函数时,我们能够访问直接函数上下文之外的变量。
回答by Nafis Ahmad
The conditional "ternary operator" (condition ? expression to evaluate when true : expression to evaluate when false
) is often used for simple conditional variable assignment.
条件“三元运算符” ( condition ? expression to evaluate when true : expression to evaluate when false
) 通常用于简单的条件变量赋值。
if you need :
如果你需要 :
if( x > 0) {
a = 10;
}else{
a = 30;
}
you can write:
你可以写:
a = (x>0)? 10 : 30;
You can think of it like a simple function, which takes 3 parameters (p1, p2, p3), if p1 is true it returns p2 and if p1 is false then it returns p3.
你可以把它想象成一个简单的函数,它接受 3 个参数(p1、p2、p3),如果 p1 为真则返回 p2,如果 p1 为假则返回 p3。
(p1)? p2 : p3;
And just like such a function, there's no way for it to cause the parentfunction to return based on the condition. It is nottherefore a shorthand for if/else.
就像这样的函数一样,它无法根据条件导致父函数返回。因此,它不是if/else 的简写。
回答by Kristian
You want to do a ternary operator
你想做一个 ternary operator
which is this:
这是这样的:
(bool) ? ifTrue : ifFalse;
Please note: you cannot omit the else portion of a ternary operator.
请注意:您不能省略三元运算符的 else 部分。