JavaScript的短路(奇怪的使用II /或操作员) [英] Javascript Short-Circuit (Strange use of II / OR operator)

查看:94
本文介绍了JavaScript的短路(奇怪的使用II /或操作员)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我跑到这片code的:

 <一个NG-点击=item.statusId == itemStatus.in ||转移()!>

我想我们可以概括为:

 <元素NG-点击=someVar == || someValue中doStuff()!>

然后我发现这个文章短路和另一个更多的聚焦对象,一旦我知道他们叫什么。不过,我还是不明白这一点。

是否基本上就结束评估,如果第一个语句的计算结果为真OR语句的工作原理?因此,如果第一个说法是正确的,最终的评价,如果是假的,在OR语句下半年运行功能? (这是我要问的主要问题,其他一切都是多余的)。

我想这部分我不明白的是是否编译器间$ P $点这个code不同或仍计算为假,只是运行的功能。甚至不知道如何短语问:


解决方案

  

是否基本上就结束评估,如果第一个语句的计算结果为真OR语句的工作原理?因此,如果第一个说法是正确的,最终的评价,如果是假的,在OR语句下半年运行功能?


它有效速记if语句,你直觉正确的,为什么它的工作原理。对于JavaScript脚本运行短路计算的更多详细信息,请参阅this堆栈溢出问题

I ran into this piece of code:

<a  ng-click= "item.statusId !== itemStatus.in || transfer()">

I guess we can generalize as:

<element ng-click = "someVar !== someValue || doStuff()">

I then found this article on short circuits and another more focused one once I knew what they are called. However, I still don't get it.

Does it basically work on the principle of an OR statement ending evaluation if the first statement evaluates to true? So if the first statement is true, end evaluation, if it's false, run the function in the second half of the OR statement? (This is the main question I am asking, everything else is extra).

I guess that part I don't get is whether the compiler interprets this code differently or it still evaluates to false and just runs the function. Not even sure how to phrase Q.

解决方案

Does it basically work on the principle of an OR statement ending evaluation if the first statement evaluates to true? So if the first statement is true, end evaluation, if it's false, run the function in the second half of the OR statement?

It's effectively shorthand for an if statement, and you intuited correctly why it works. For more details on short-circuit evaluation in JavaScript, see this Stack Overflow question.

这篇关于JavaScript的短路(奇怪的使用II /或操作员)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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