首頁  >  文章  >  web前端  >  Javascript三種條件表達的寫法的特徵及用法實例詳解

Javascript三種條件表達的寫法的特徵及用法實例詳解

伊谢尔伦
伊谢尔伦原創
2017-07-18 13:55:231468瀏覽

三種條件表達的寫法的特點及用處進行了總結歸納,簡述如下:

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