简体   繁体   English

是否有可以缩短条件语句之类的if语句的语法?

[英]Is there the syntax to shorten if statement like conditional operator?

I want to write this code more short: 我想更简短地编写以下代码:

var fn = function (a) {
    if (a) {
        somefn.call(null, a);
    }
};

Using arrow statement, it will be: 使用箭头语句,它将是:

var fn = (a) => { if (a) somefn.call(null, a); };

I want more short code, if using conditional operator: 如果使用条件运算符,我需要更多短代码:

var fn = (a) => a ? somefn.call(null, a) : null;// null is dummy code

It is not beautiful. 它不漂亮。 Is there any syntax like this?: 有这样的语法吗?:

var fn = (a) => a ? somefn.call(null, a);

thanks. 谢谢。

如果a为真,则可以使用逻辑AND && ,它检查a并调用该函数。

var fn = a => a && somefn.call(null, a);

Your original code is short enough, "beautiful", and hopefully do what you want. 您的原始代码足够简短,“漂亮”,并希望您可以执行所需的操作。

You can write something like this. 您可以这样写。 But this is not better code, uglier, hard to understand... 但这不是更好的代码,更丑陋,难以理解...

var fn = a ? function( a ) { somefn.call(null, a) ; } : function( ) { };

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

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