如何在JavaScript中评估三元运算符? [英] How is the ternary operator evaluated in JavaScript?
本文介绍了如何在JavaScript中评估三元运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
关于JavaScript中的三元(?:
)运算符,我想知道典型的浏览器的JavaScript解释器如何评估它:
Regarding the ternary (? :
) operator in JavaScript, I would like to know how it is evaluated by a typical browser's JavaScript interpreter:
替代项A:
- 求第一个操作数。
- 如果第一个操作数的结果为true,然后求值并返回第二个操作数。
- 否则,求值并返回第三个操作数。
备用B:
- 对所有三个操作数求值。
- 如果第一个操作数的结果为true,则返回第二个操作数的结果。
- 否则,返回第三个操作数的结果。
备选C:
当然,如果备选方案A和备选方案B均不能准确描述三元运算符的工作方式,请解释一下它的工作原理。
Of course, if neither alternative A nor alternative B accurately describe how the ternary operator works, please explain me how it works.
推荐答案
替代A:
(1)? functionOne(): functionTwo()
如果在两个函数上都添加了简单的警报消息,则只有functionOne会显示其消息。
If you put a simple alert message on both functions, only functionOne will display its message.
function functionOne(){
alert("one");
}
function functionTwo(){
alert("two");
}
这篇关于如何在JavaScript中评估三元运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文