简体   繁体   English

Javascript 速记 if-else 并返回

[英]Javascript shorthand if-else and returning

Can the javascript shorthand for if-else return out of a function?是否可以从 function 中返回 if-else 的 javascript 简写? If so how would this work.如果是这样,这将如何工作。

eg.例如。 I have this:我有这个:

if(boolean){
 return;
}

and I would like to write it as this:我想这样写:

(value)? return;

Chrome complains that return is unexpected. Chrome 抱怨返回是意外的。 Is there anyway to write something like this so that it is valid?反正有没有写这样的东西使它有效?

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来停止函数的执行。

If you intend to return from the function at this point in its execution regardless of 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: 但是,如果您只是希望在测试评估为true时提前返回(例如,作为一个完整性检查以防止在参数无效时执行),您可以做到的最短时间是:

if (boolean) return;
if(boolean) return;

单行,可读,完全有效;

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. 当我们使用箭头函数时,我们能够从直接函数上下文中访问变量。

if I'm not mistaken you're talking about this right?如果我没记错的话,你说的是这个吧? a short-hand.简写。

 return (if the value here is true then ->) && false

example:例子:

const isGood = true && "yeah";
console.log(isGood) //yeah

Use this as your short hand if the value is true then it will return false;如果值为 true,则将其用作您的简写,那么它将返回 false; using the && operator is better使用 && 运算符更好

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部分。

http://en.wikipedia.org/wiki/Ternary_operation http://en.wikipedia.org/wiki/Ternary_operation

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 parent function to return based on the condition. 就像这样的函数一样,它没有办法让函数根据条件返回。 It is not therefore a shorthand for if/else. 因此它不是 if / else的简写。

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

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