PHP8.1.21版本已发布
vue8.1.21版本已发布
jquery8.1.21版本已发布

Javascript三种条件表达的写法的特点及用法实例详解

伊谢尔伦
伊谢尔伦 原创
2017-07-18 13:55:23 1214浏览

三种条件表达的写法的特点及用处进行了总结归纳,简述如下:

1. if...else结构


// Set r to 0 or 1 
var r= Math.floor(2*Math.random()) 
// Set a, b and c to "small" if r==0 an else set them to "big" 
// using three different techniques 
// Method 1: If else 
var a; if (r==0){a = "small"} else {a = "big"}; 
// Method 2: Conditional operator 
var b = r==0 ? "small" : "big"; 
 
// Method 3: And/or operators 
var c = r==0 && "small" || "big"; 
// Check the values of our variables 
alert(r+" "+a+" "+b+" "+c);

2. if...else if...else结构


// Set r to 0,1,2 or 3 
var r= Math.floor(4*Math.random())  
// Set a, b and c to "nada","small","big" and "huge" 
// depending on the value or r using three different techniques 
// Method 1: If.. else if... else 
var a; 
if (r==0){a="nada"} 
else if (r==1){a="small"} 
else if (r==2){a="big"} 
else {a="huge"}; 
// Method 2: Conditional operators 
var b = 
r==0 ? "nada" 
: r==1 ? "small" 
: r==2 ? "big" 
: "huge";  
// Method 3: And/or operators 
var c = 
r==0 && "nada" 
|| r==1 && "small" 
|| r==2 && "big" 
|| "huge";  
// Check the values of our variables 
alert(r+" "+a+" "+b+" "+c);

3. 执行函数


// Set r to 0,1,2 or 3 
var r= Math.floor(4*Math.random()) 

// The global variable x and our four functions 
var x=""; 
nada=function(){x+="Nada! "}; 
small=function(){x+="Small! "}; 
big=function(){x+="Big! "}; 
huge=function(){x+="Huge! "}; 
// Call a specific function depending on the value of r 
// using three different techniques  
// Method 1: If.. else if... else 
if (r==0){nada()} 
else if (r==1){small()} 
else if (r==2){big()} 
else {huge()};  
// Method 2: Conditional operators 
r==0 ? nada() 
: r==1 ? small() 
: r==2 ? big() 
: huge(); 
// Method 3: And/or operators 
r==0 && (nada() || true) //nada()函数不一定返回true,为了保证后续的逻辑或||判断不被执行,需要返回true值,下同
|| r==1 && (small() || true) 
|| r==2 && (big() || true) 
|| huge();  
// Check the values of our variables 
alert(r+" "+x);

4. 执行代码


// Set r to 0,1,2 or 3 
var r= Math.floor(4*Math.random())  
// The global variable x 
var x=""; 
// Executing different code depending on the value of r 
// using three different techniques  
// Method 1: If.. else if... else 
if (r==0){x+="Nada! "} 
else if (r==1){x+="Small! "} 
else if (r==2){x+="Big! "} 
else {x+="Huge! "}; 
// Method 2: Conditional operators 
r==0 ? function(){x+="Nada! "}() 
: r==1 ? function(){x+="Small! "}() 
: r==2 ? function(){x+="Big! "}() 
: function(){x+="Huge! "}(); 
// Method 3: And/or operators 
r==0 && (function(){x+="Nada! "}() || true) 
//有人在评论中指出这里的匿名函数是不必需的,在只有一条可执行代码时是这样的,但是如果有多条代码需要执行,匿名函数还是不错的
|| r==1 && (function(){x+="Small! "}() || true) 
|| r==2 && (function(){x+="Big! "}() || true) 
|| function(){x+="Huge! "}();  
// Check the values of our variables 
alert(r+" "+x);

在只存在两种条件的判断中,用if...else或?:都是相当直白,而&&和||的运算方式就稍嫌复杂。但是其实只要明白以下两个基本原则,所有问题都会迎刃而解了:

其一、当用逻辑与&&和逻辑或||运算符运算时,方向都是自左向右的,&&运算到第一个值为false的条件(或可转换为false的值,如null/undefined/0/""/NaN等)时停止,而运算到第一个值为true的条件(或可转换为true的值)时停止;整个条件返回的值是最后检测的条件的值,不一定只是true/false。

其二、逻辑与&&运算符较逻辑或运算符相比,前者有更高的优先级。

以上就是Javascript三种条件表达的写法的特点及用法实例详解的详细内容,更多请关注php中文网其它相关文章!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。