gpt4 book ai didi

javascript - 编写 if 语句的新方法

转载 作者:行者123 更新时间:2023-11-30 07:17:08 27 4
gpt4 key购买 nike

我在搜索网站的源代码时找到了一种以另一种方式(我认为)编写 if 语句的方法。

代替:

if(a)b;

或:

a?b:'';

我读了:

!a||b;

第三种方式和前两种方式一样吗?如果是,为什么我们要使用第三种方式?

最佳答案

第三种方式和前面的一样。使用它的一个理由是节省字节。反对使用它的一个有力论据是可读性。您最好在编写代码时关注可读性,并使用最小化器(例如 Google Closure Compiler )来节省字节。

还可以更短:

a && b;
/* !a||b means:
(not a) OR b
which is equivalent to
a AND b
which turns out to be
a && b
*/

关于javascript - 编写 if 语句的新方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11121193/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com