三元运算符,猫王运算符,安全导航运算符和逻辑或运算符的比较 [英] Comparison of Ternary operator, Elvis operator, safe Navigation Operator and logical OR operators

查看:183
本文介绍了三元运算符,猫王运算符,安全导航运算符和逻辑或运算符的比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

let gender = user.male ? "male" : "female";


猫王运算符(?:)

let displayName = user.name ?: "";


安全导航操作员(?.)

let displayName = user.name ?. "";


逻辑或运算符

let displayName = user.name || "";


以上所有运算符都类似地执行相同的过程.有什么不同的好处,最好的是哪一项?最糟糕?

The above all operators are similarly doing the same process. What are the difference benefits and which one is best & worst?

推荐答案

更新

使用 TypeScript 3.7 他们已经实现了Optional Chaining(可选链接),就像安全导航操作员一样,但是效果更好.还有 Nullish Coalescing 已找到出路.

With TypeScript 3.7 they've implemented Optional Chaining, which is like the safe navigation operator, but then better. Also the Nullish Coalescing has found its way to the scene.

如果您使用的是此版本,则以下答案已过时

If you are using this version, the below answer is obsolete

也许我错过了几个版本,但是据我所知,TypeScript没有Elvis运算符或安全的导航运算符.他们唯一的额外东西是一个非null的断言运算符!.,但这仅适用于编译器,在已编译的js中,!将被删除.但是,Angular的模板中确实包含安全的导航操作符,但是在后台,这将解析为逻辑或||.这样做的好处是提高了可读性和较小的模板.

Maybe I've missed a couple versions, but to my knowledge, TypeScript does not have an elvis operator or a safe navigation operator. The only extra thing they have is a non-null assertion operator !., but this is only for the compiler, and in the compiled js the ! will be removed. Angular however, does have the safe navigation operator inside their templates, but under the hood this will resolve into a logical or ||. The benefit here is increased readability and smaller templates.

除此之外,TypeScript确实具有?:表示法,但是在接口或方法参数中使用该表示法表示该值是可选的

Besides that, TypeScript does have the ?: notation, but this is used in interfaces or method parameters to indicate that the value is optional

哪个让我们留给三元运算符vs逻辑或.如果有3个值,则将使用第一个.问题,答案为是,答案为否.

Which leaves us with the ternary operator vs logical or. You would use the first one if there are 3 values. The question, the answer yes result, and the answer no result to said question.

当有2个值时为后者.答案是真实的,第一个答案将是答案,否则第二个答案将是答案,无论其价值如何.

And the latter when there are 2 values. Where the first one, if resolved to truthy will be the answer, and otherwise the second one, regardless of its value.

明智的选择,我不能说太多.我希望它们能以同样快的速度运行,但有一点点差异.也许使用三元选项提高了可读性,您显然可以始终使用三元选项来代替逻辑或||,但是我个人更喜欢使用||,因为它使代码紧凑.

Benefit wise, I can't really say much. I would expect them to be equally fast, with a marginal difference. Perhaps readability is increased with the ternary option, which you obviously can always use instead of the logical or ||, but personally I like to use the ||, because it keeps the code compact.

TLDR;

  • 三元运算符

已简化,可在任何地方使用

Simplified if else, available everywhere

  • 猫王算子?:

在typescript/javascript/angular中不可用,与||

Not available in typescript/javascript/angular and essentially the same as ||

  • 安全导航操作员?.

仅在角度模板中可用,用于防止对象参数导航中出现空指针

Only available in angular templating, used to prevent null pointers in object parameter navigation

  • 逻辑或||

如果不是左手,则为右手.如果没有的话,甚至更加简化.在typescript/javascript/angular

If not left hand, then right hand. Even more simplified if else. Available in typescript/javascript/angular

这篇关于三元运算符,猫王运算符,安全导航运算符和逻辑或运算符的比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆