js中andalso用法_ANDALSO-ORELSE的简单性。

js中andalso用法

This is one of the simple operators yet less used due to clarity over its purpose. Let’s looks at it

这是简单的运算符之一,但由于其目的明确而很少使用。 我们来看一下

.ANDALSO :

Take an example where we are evaluating the business logic.

以我们评估业务逻辑为例。

Img1
Output 输出量
Img2

Case 1: In this case program will execute the IsValidUser function, it returns valid and then execute the HasAccessToUpdateOrders function which is false and everything is ok.

情况1:在这种情况下,程序将执行IsValidUser函数,返回有效值,然后执行false且一切正常的HasAccessToUpdateOrders函数。

Case 2. Consider  here IsValidUser returns false then logically there is no way  that user might have any Access to update Orders, however the programs still executes the function  HasAccessToUpdateOrders. Logically this should have been avoided, unnecessary execution consuming resources and time.  Hence when we use AND both sides of expression will be executed before considering the final result.

情况2。在这里考虑IsValidUser返回false,然后从逻辑上讲,用户不可能有任何访问权限来更新Orders,但是程序仍然执行HasAccessToUpdateOrders函数。 从逻辑上讲,应该避免这种情况,不必要的执行会浪费资源和时间。 因此,当我们使用AND时,将在考虑最终结果之前执行表达式的两端。

So to avoid this we have short-circuiting, so now the same stuff with ANDALSO. And the output seems to evaluate only the first part since it’s false it doesn’t evaluate the second expression and improves the performance.

因此,为避免这种情况,我们会发生短路,因此现在与ANDALSO相同。 输出似乎只评估第一部分,因为它为假,它不评估第二个表达式并提高了性能。

Img3
Output: 输出:
Img4
2. ORELSE:2. ORELSE:

Taking same example from above but instead of IsUser we consider IsAdmin, so if user is Admin then he should have access to update orders no need to check further.

从上面以相同的示例为例,但是我们不是IsUser,而是考虑IsAdmin,因此,如果用户是Admin,则他应该有权更新订单,而无需进一步检查。

Img5

Output:

输出:

Img6

Case 3: In this case program will execute the IsAdmin function, it returns false and then execute the HasAccessToUpdateOrders function which is true and everything is ok.

情况3:在这种情况下,程序将执行IsAdmin函数,返回false,然后执行HasAccessToUpdateOrders函数,该函数为true,一切正常。

Case 4: Consider  here IsAdmin returns true then logically  user have all access to update Orders, however the programs still executes the function  HasAccessToUpdateOrders. Logically this should have been avoided, unnecessary execution consuming resources and time.  Hence when we use OR both sides of expression will be executed before considering the final result.

情况4:此处考虑IsAdmin返回true,然后逻辑上用户具有对更新Orders的所有访问权限,但是程序仍执行HasAccessToUpdateOrders函数。 从逻辑上讲,应该避免这种情况,不必要的执行会浪费资源和时间。 因此,当我们使用OR时,将在考虑最终结果之前执行表达式的两端。

So to avoid this we have short-circuiting, so now the same stuff with ORELSE. And the output seems to evaluate only the first part since it’s true it doesn’t evaluate the second expression and save redundant expression evaluation.

因此,为避免这种情况,我们会发生短路,因此现在与ORELSE相同。 输出似乎只评估第一部分,因为它确实不评估第二个表达式并保存了多余的表达式评估。

Img7

Output:

输出:

Img8

C# Equivalent:

等效于C#:

AndAlso  && (C# Equivalent)

AndAlso &&(等效于C#)

OrElse     ||   (C# Equivalent)

OrElse || (等效于C#)

Conclusion :

结论:

AndAlso & OrElse can help improve performance by avoiding unnecessary code execution or database hits.

AndAlso&OrElse可以避免不必要的代码执行或数据库命中,从而有助于提高性能。

It can also be used to check if object is null, to avoid runtime errors.

它还可以用于检查object是否为null,以避免运行时错误。

Ex:

例如:

If Not myObject  Is Nothing AndAlso myObject.Count Then…

翻译自: https://www.experts-exchange.com/articles/11089/ANDALSO-ORELSE-Simplicity.html

js中andalso用法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值