第 七 章 Operators

德嘉書業 / 德嘉資訊科技 (www.takka.com.hk) 作者: 伍新華 Email: ng-sun-wah@graduate.hku.hk

7.1 Assignment operator

7.2 Arithmetic operators

7.3 Comparison operators

7.4 Logical operators

7.5 Bitwise AND operator : &

7.6 Bitwise OR operator : |

7.7 String operator

7.8 Special operators

1. conditional operator ( ? : )
2. comma operator
3. delete operator
4. new operator
5. this operator
6. typeof operator
7. void operator

7.9 Operators 的優先處理次序


 

  在程式語言中, 指示程式進行運算 (計算、比較或連結) 的符號, 稱為 operators (運算子), 被運算的資料稱為 operands (運算元), 一句中有 operators 及 operands 就稱為 expression

  以 if...else statement 為例, 在 if(condition) 這一句, 實際有以下語法:

if(expression)
    {  statement1 }
 else  {  statement2  }

  這處的 expression 就是設定 condition 的語句, 例如 x==y 就是一個 expression, 是檢查 x 是否等於 y , == 是 operator , xy 是 operands。

  JavaScript 可使用以下的 operators:

 Assignment operators
 Arithmetic operators
 Comparison operators
 Logical operators
 Bitwise operators
 String operators
 Special operators
   ?: (Conditional operator)
   , (Comma operator)
   delete
   new
   this
   typeof
   void

 

 

7.1 Assignment operator

  在 JavaScript, assignment 有兩個用途:

1. 用來將一個 property 指定變為某一個屬性, 例如:

document.bgColor="yellow"    ( 這是將網頁的背景顏色變為黃色 )
window.location="index.htm"    ( 這是載入 index.htm 這網頁 )

  本書很多地方都說到 assignment 在這方面的操作。

 

2. 在一般程式中, assignment operator 是 "配值運算子", 在 5.1 的一節已說過這方面的操作, 是使用 = 這符號將右方的值配給左方的變數。

 

使用 += :

  = 是基本符號, 我們也可以將這符號與 + - * / (加減乘除) 合用, 例如:

   a += b  是等於  a = a + b
   a -= b  是等於  a = a - b

  有關這些簡便寫法, 請看 附錄-9-2 的一欄, 例如以下的script會顯示 30 這數值。

a = 10
b = 20
b += a      
(這是等於 b = b + a )
document.write(
b)

 

  這 += 符號也可用於文字, 例如以下的 script 會顯示: Hello, Good morning!

a = "Good morning! "
b
= "Hello, "
b
+= a    
(這是等於 b = b + a )
document.write(b)

 

 


7.2 Arithmetic operators

  Arithmetic operators 是 "算術運算子", 用作四則運算, 有以下各項:

Operator  名 稱 用 法t
+ 加 (add) a + b
- 減 (subtract) a - b
* 乘 (multiply) a * b
/ 除 (divide) a / b
% 餘數 (modulus) a % b ( a 除以 b 得出的餘數)
++ 遞增 (increment) i++   ++i
-- 遞減 (decrement) i--   -i
- 負號 (negation) -a

 

餘數:
  % 是餘數 (modulus) 的符號, 是用來取出一個除數中的餘數, 例如 13%5 , 餘數是 3, 所以設定 a = 13 % 5, a 就會等於 3。

 

increment/decrement operators:

  increment operator (遞增運算元) 是將一個變數加一, 多用於迴圈的計次, 這變數習慣用 i 來代表, 例如 i++ , 請看 6.7 的一節。i++ 也可寫成 ++ii=i+1

  decrement operator (遞減運算元) 是將一個變數減一, 例如 i-- , 這也可寫成 --i i = i - 1, 例如以下的 script:

<script>
for (
i =10 ; i > 0 ; i-- )
 {   document.write(
i + " ")
   --
i
 }
</script>

  這處有一個 i-- 及一個 --i , 所以每個迴圈減 2, 得出 10 8 6 4 2  的結果, 若你使用 alert(i ) , 就可看到每個迴圈中 i 數值的變化。

 

算術運算子的優先處理次序:

  各算術運算子在處理上的優先次序 (precedence) 是依一般算術規則: 先乘除, 後加減, 若有一些運算要優先處理, 可放在 ( ) 內, 例如:

       3 + 4 / 2 = 5
     (3+4) / 2 = 3.5

 

 


7.3 Comparison operators

  這類是 comparison operators (比較運算子) 或 relational operators (關係運算子), 用問題方式來比較兩個數值, 例如 ab 兩個數值, 這類運算子會問 a 是否大過 b, 或問 a 是否細過 b, 如下:

Operator Comparison
a>b Is a "greater than" b ? ( a 大於 b ? )
a<b Is a "less than" b ? ( a 少於 b ? )
a>=b Is a "greater than or equal to" b ? ( a 大於或等於 b ? )
a<=b Is a "less than or equal to" b ? ( a 少於或等於 b ? )
a==b Is a "equal to" b ? ( a 等於 b ? )
a!=b Is a "not equal to" b ? ( a 不等於 b ? )

 

  上表中 6 個 operator , 答案只有 TRUE 或 FALSE 兩個可能性 (是為 Boolean value), 請參看 6.1 的一節。

 

  這處筆者要重複說明 == (equal) 與 = (assignment) 的分別, 因為初學程式者都會一時大意犯錯, 例如:

a=b+5 這是 assignment statement, 是將 b5 的結果變為 a 的新值。
a==b+5 這是 comparison expression, 是看 a 是否等於 b+5, 一般是與 if statement 合用, 例如 if(a==b+5)

 

true 及 false 的傳回:

  使用一個 comparison operator 時, 這 operator 會傳回 true 或 false 這兩個值, 有時我們可以利用這兩個字作進一步的操作, 請看以下例子:

<script>
a = (5 == 1 )      這會得出 a=false 的結果
b = (5 > 1 )       這會得出 b=true 的結果
document.write( a + " / " + b )
</script>

  (5 == 1 ) 是 FALSE, 所以會產生 false 這個字, 同時會有 a=false 這 assignment, a 是等於 false, 同樣原理, b=true, 所以這 script 會傳回這兩個字: false / true

  留意 a=(5==1) 這句有兩個 operators: === , 因 comparison operator 較 assignment operator 有優先權, 因此也可將 a=(5==1) 寫成 a=5==1, 但將 5==1 放在括號內, 就很明確地指定這部份優先處理。

 

  在 JavaScript, FALSE 是等於 0 , TRUE 是等於 1, 在上述的 script, 若以數字來顯示 (請看 4.6-5 的一段), 例如作以下的修改:

<script>
a = (5 == 1 )
b = (5 > 1 )
a = Number(a)
b
= Number(b)
document.write(
a + " / " + b )
</script>

  這是以 Number( ) 來將 ab 作數字來處理 , 所以這兩個變數會傳回這兩個數字: 0 / 1, 你可試試 document.write( (5==5) + (3>1) + (4>1) ) 這一句, 看得出什麼數值。

 

 


7.4 Logical operators

  這是 "邏輯運算子", 用來結連兩個 comparison operators, 格式是 "expression1 && expression2"

 

1. Logical AND operator: &&

  例如 age 是一個數值, 一個程式要檢查輸入的 age 數值是否介乎 20 與 40 之間, 這檢查分為兩個問題:

  Test 1 :   Is age greater than or equal to 20 ?  ( age >= 20 )
  Test 2 :   Is age less than or equal to 40 ?  ( age <= 40 )

  這兩項檢查可使用 AND 這一個邏輯運算元來合併, 使用的符號是 && (double ampersand), 如下:

Test 1 && Test 2 Combined result
TRUE && TRUE   TRUE
TRUE && FALSE   FALSE
FALSE && TRUE   FALSE
FALSE && FALSE   FALSE

  請看以下例子。

 

練習-51 && operator

  這練習的網頁有一個文字框讓觀看者輸入年歲, 若數值是介乎 20 至 40 之間 (這是 TRUE), 有對話盒顯示 "你符合資格。" 這句, 否則就是 FALSE, 有對話盒顯示 "年齡少於 20 或大於 40 不符合資格。"

1. 請用瀏覽器開啟示範磁碟中的 and.htm, 這檔案有以下內容:

<html> <body>

<form name=fm>
請輸入你的年歲:
<input type=text name=tx size=4>
<input type=button value="
確定" onClick="checkIt( )" >
</form>

<script>
function
exam( )
{  if( document.
fm.tx.value >= 20 && document.fm.tx.value <= 40)
    alert("
你符合資格。")
 else  alert("
年齡少於 20 或大於 40 不符合資格。")
}
</script>

</body> </html>

2. 請你在網頁的文字框輸入不同的數值, 例如 19、20、40、41 等等, 按 [確定], 看有什麼反應。

 

  這處的 if statement 的條件是由以下兩個 tests 組成:

    test 1              test 2
    ↓              ↓
document.fm.tx.value >= 20 && document.fm.tx.value <= 40

  這兩個 tests 用 && 來結合, 兩個都是 TRUE, 這 if statement 才會是 TRUE, 否則就是 FALSE。

 

2. Logical OR operator: ||

  若一個程式要檢查輸入的 age 數值是否少於或等於 19 "或" 大於或等於 41 (即是摒除 20 與 40 之間的數值), 這檢查分為兩個問題:

    Test 1 :   Is age less than or equal to 19 ?  ( age <= 19 )
    Test 2 :   Is age greater than or equal to 41 ?  ( age >= 41 )

 

  這兩項檢查可使用 OR 這一個邏輯運算元來合併, 使用的符號是 || (double pipe), 如下:

Test 1 || Test 2 Combined result
TRUE || TRUE   TRUE
TRUE || FALSE   TRUE
FALSE || TRUE   TRUE
FALSE || FALSE   FALSE

 

  當兩個 tests 其中一個是 TRUE, || operator 的結果就會是 TRUE, 兩個 FALSE 的結果才會是 FALSE。

 

練習-52  || operator

  這練習有一個文字框讓觀看者輸入年歲, 若數值是少於或等於 19, 又或是大於或等於 41, 都會是 TRUE, 有對話盒顯示 "你符合資格。" 這句, 否則就是 FALSE, 有對話盒顯示 "年齡介乎 20 與 40 之間不符合資格。"

1. 請用瀏覽器開啟示範磁碟中的 or.htm, 這檔案有以下內容:

<html> <body>

<form name=fm>
請輸入你的年歲:
<input type=text name=tx size=4>
<input type=button value="
確定" onClick="checkIt( )" >
</form>

<script>
function
exam( )
{ if ( document.
fm.tx.value <=19 || document.fm.tx.value >=41 )
     alert("
你符合資格。")
  else  alert("
年齡介乎 20 與 40 之間不符合資格。")
}
</script>

</body> </html>

2. 請你在網頁的文字框輸入不同的數值, 例如 19、20、40、41 等等, 按 [確定], 看有什麼反應。

 

 

3. Logical NOT operator: !

  這是相反的 operator, 格式是 !expression, 若這 expression 是 FALSE,
!expressin 會得出 TRUE 的結果, 若 expression 是 TRUE, 會得出 FALSE 的結果, 例如以下程式:

<script>
a = (5==5)
b = !(5==5)
document.write(
a + " / " + b )
</script>

  這會傳回 true / false 這兩個字。

 

 


7.5 Bitwise AND operator : &

  這是將兩個數字轉為二進位數字, 然後將兩個數字同位置的位元比較, 使用以下的 TRUE-FALSE table:

Test 1 & Test 2 Combined result
  1 & 1   1
  1 & 0   0
  0 & 1   0
  0 & 0   0

  這與前面的 && operator 的 TRUE-FALSE table 一樣, 只是轉了用 0 代表FALSE, 1 代表 TRUE。

 

  例如 6 的二進位數字是 0110, 4 是 0100, 假定 a = 6 , a & 4 會有以下比較:

 

 

  你可試試以下的 script, 看是否得出 4 的結果。

<script>
a = 6
x = ( a & 4 )
document.write( "
a & 4 的結果是: " + x )
</script>

 

  Bitwise operators 常在 C 語言用作硬件的控制, 在 10.3 的一節會說到 set flag 這類操作, 例如 a 的數值是介乎 0 至 15, 就會有 4 個 bit 的位置, 若 a 是 12 (1100), 就會有以下數字:

 

  例如你用 a 這變數的 bit-2 來做 flag, 其他 bit-0、bit-1、bit-3 就變成無關重要, 你有興趣知道的是 bit-2 是 0 或是 1, 其中一個方法是用 a&4 這一個檢查, 若結果是 4 (0100) 表示 a 的 bit-2 是 1, 若結果是 0 (0000), 表示 a 的 bit-2 是 0。

  你可試試以下的 script 看有什麼效果:

<script>
a = 10
x = ( a & 4 )
if (
x == 4)   alert ("a 的 bit-2 是: 1")
if (
x == 0 )  alert ("a 的 bit-2 是: 0" )
</script>

 

  你可更改 a 的數值, 例如 2 (0010)、3 (0011)、5 (0101)、8 (1000) 、9 (1001) 等等, 看這方法是否真的能檢查到 a 的 bit-2 是 0 或是 1。

 

 


7.6 Bitwise OR operator : |

  這是將兩個數字轉為二進位數字, 然後將兩個數字同位置的位元比較, 使用以下的 TRUE-FALSE table:

Test 1 | Test 2 Combined result
  1 | 1   1
  1 | 0   1
  0 | 1   1
  0 | 0   0

  這與前面的 || operator 的 TRUE-FALSE table 一樣, 只是轉了用 0 代表 FALSE, 1 代表 TRUE。

   一般的 JavaScript 很少會用到 bitwise operators, 所以筆者不再說下去。

 

 


7.7 String operator

  這也稱為 concatenation operator (文字連結器), 是利用 + 這符號來連結兩項文字, 例如 "今天 " + "天氣 " 會變為 "今天天氣 " , 在前面有關 document.write( ) 的使用中, 已有很多例子。

  這 + 的符號除了作為文字連結器, 還作為加的符號, 例如 3 + 4 會變為 7 , 若是作文字連結器使用, 就要將文字放在 " " 內, 請看 練習-42 的說明。

  有時瀏覽器遇到 + 這符號時, 是不知你要將這符號作為文字連結器或作加號來使用, 請看以下例子:

 

練習-53 使用 + 符號

  這練習的網頁有兩個文字框, 你在這兩個文字框輸入數字, 然後用一個 alert 對話盒顯示兩個數字相加的結果, 看有什麼效果。

1. 請用瀏覽器開啟示範磁碟中的 plus.htm, 這檔案有以下內容:

<html> <body>

<form name=fm> 請在兩個文字框輸入數字:
<input type=text name="box1 " size=4>
<input type=text name="
box2 " size=4>
<input type=button value="
兩數字相加 " onClick="checkIt( )">
</form>

<script>
function
checkIt( )
{ 
x = document.fm.box1.value
  y
= document.fm.box2.value
  a
= (x + y )
  alert("
總數是: " + a )
}
</script>

</body> </html>

2. 請你在兩個文字框輸入數字, 然後按 [兩數字相加] 的按鈕, 看有什麼結果 (結果是的)。

 

1. 例如你在兩個文字框分別輸入 12 及 24, 因此 x = 12, y = 24, 你會發現 a = x+y 的結果是 1224 , 而不是 36 , 瀏覽器將這 + 符號當作文字連結器來使用。

2. 在一些模糊情況, 我們要給瀏覽器明確的指示, 瀏覽器才知道如何處理, 在這例子, 我們可以用以下方法:

1. 使用 eval( ) , 例如: a = eval(x) + eval(y)

2. 使用 Number( ) , 例如: a = Number(x) + Number(y)

3. 若是整數, 可使用 parseInt( ) , 例如 a=parseInt(x) + parseInt(y)

4. 有小數可使用 parseFloat( ) , 例如 a=parseFloat(x) + parseFloat(y)

5. 可將數值減 0, 瀏覽器見到有減數, 就知道這是數字, 這不是正統方法, 但也可行, 例如: a = ( x - 0) + ( y - 0 ) , 不過因減 0 的關係, 若 xy 有小數位, 會出現浮點數誤差 (請看 18.2-1 的一段)。

 

 


7.8 Special operators

 

1. conditional operator ( ? : )

  這是一個簡便的 if(condition) statement, 有以下語法:

(condition? 變數1 : 變數 2 )  或 (condition? "文字1" : "文字2")

  若這 condition 是 TRUE, 上述的一句就會用 變數 1 來取代, 若是 FALSE, 就會用 變數 2 來取代, 若 變數 1變數 2 是文字, 就要放在 " " 內。請看以下簡單的例子:

<html> <body>
<script>
A=10
B=5
document.write("
A is " + ( A>B ? 'greater ' : 'smaller ') + " than B " )
</script>
</body> </html>

 

請看這句:  

 

  這處的條件是 A>B ? ( A 是否大過 B ), 答案有兩個可能性:

1. 若是 TRUE, ( A > B ? 'greater' : 'smaller' ) 變成 'greater'

2. 若是 FALSE, ( A > B ? 'greater' : 'smaller' ) 變成 'smaller'

 

練習-54 使用 ? : 符號

  這練習是重複 練習-45 的 prompt 對話盒, 今次使用 ? : 這個檢查方式來決定顯示的資料。

1. 請用瀏覽器開啟示範磁碟中的 cond.htm, 這檔案有以下內容:

<html> <body>
<form>
<input type=button name="" value="
再 玩 " onClick=play( ) >
</form>

<script>
function play( )
{ 
x=Math.round(Math.random()*100 )
  y
=Math.round(Math.random()*100 )

 entry=promptt( x + " " + y + " 是多少?","") 
 if (
entry==null || entry=="" )
     { alertt("
你沒有計算!")  }
   else { alert( (
entry==x+y? "好! 你答對了。" : "噢! 你計錯了。") ) }
}
play( )
</script>

</body> </html>

2. 網頁開啟後, 你會見到一個 prompt 對話盒, 請輸入正確及錯誤的答案, 看走出來的對話盒有什麼顯示。

 

 

2. comma operator

  假若我們要在一個 statement 中使用兩個或以上的 expressions, 可用 , 來分隔這些 expressions, 這就是 comma operator, 常用於 for loop, 使到一個 loop 內可以進行超過一項運算。

  在這例子, 你看到如何使用一些 for loop 在畫面顯示以下 10 行文字, 第一行顯示 10 個 S 字, 1個 T 字, 第二行 9 個 S 字, 2 個 T字, 如下:

S S S S S S S S S S T
S S S S S S S S S T T
S S S S S S S S T T T
S S S S S S S T T T T
S S S S S S T T T T T
S S S S S T T T T T T
S S S S T T T T T T T
S S S T T T T T T T T
S S T T T T T T T T T
S T T T T T T T T T T

 

  請你首先試試以下的 script, 這是在示範磁碟中的 comma1.htm:

<script>
for (
x=10; x >= 1; x--)
 {   for(a=x; a>=1; a--)
      { document.write("
S ") }
   document.write(" <br> ")
 }
</script>

  在第一個迴圈, x=10, 所以 for(a=x; a>=1; a--) 變成 for(a=10; a>=1; a--), 這會印出 10 個 S。

  在第二個迴圈, x=9, 所以 for(a=x; a>=1; a--) 變成 for(a=9; a>=1; a--), 這會印出 9 個 S , 這迴圈一路重複, 直至 x=1, 在最後一行印出 1 個 S。

 

  請你試以下第二個 script, 這是在示範磁碟中的 comma2.htm :

<script>
for (
y=1; y<=10; y++ )
 { for (
b=1; b<=y; b++)    {   document.write(" T ")  }
   document.write("<br>")
 }
</script>

  這會在第 1 行顯示 1 個 T 字, 第 2 行 2 個 T 字, 到第 10 行就顯示 10 個 T 字。

 

  你明白了以上兩個 script 的原理, 就可在 for loop 中使用兩個 expressions, 一個印出 S 字, 另一個印出 T 字, 請看以下 script, 這是在示範磁碟中的 comma3.htm :

<script>
for (
x=10, y=1; x>=1; x--, y++ )
{ for(
a=x; a>=1; a--)   { document.write(" S ")  }
  for(
b=1; b<=y; b++)  { document.write(" T ")  }
  document.write("<br>")
}
</script>

  在今次例子, 請留意這句: for (x=10, y=1; x>=1; x--, y++ )

  x=10, y=1 是同時設定 x y 的始值, x--, y++ 同時設定 x 遞減和 y 遞增, 這是在一個 statement 內使用兩個 expressions。

  當一個 statement有兩個 expressions, 若要傳回一個 true/false 值, 使用的是第二個 expression, 例如以下的 script :

<script>
a = ( ( 7 >10 ), ( 8 > 5 ) )
alert("
The value of a is: " + a )
</script>

  以上的 script 會在 alert 對話盒中傳回 true 這字。

 

3. delete operator

  delete operator 可用來取消 array (陣列) 中某一個名稱, 留意取消一個名稱後, 陣列排序不變, 取消了的名單變成 undefined。delete 也可用來取消一個我們自訂的 object 名稱或 property。這項功能實際用途不大, 所以你也可以不理會。

 

4. new operator

  new 是用來造出一個新的 object, 最常見的是用 new Array( ) 造出一個陣列物件及用 new Date( ) 造出時間物件, 請看 Array 及 Date object 的兩章。

 

5. this operator

  this 可用來代表當時工作的物件, 一般有這語法:

 

  在一些大的 JavaScript 中, 使用 this 可簡化一個長的 object 名稱, 請看以下例子。

練習-55 this 的使用

  這練習有兩個文字框, 指定要觀看者輸入 20 至 40 間的數值, 程式中用 this 來代表這兩個文字框, 然後在一個 function 中檢查 this.value 是否介乎 20 至 40 之間。

1. 請用瀏覽器開啟示範磁碟中的 this.htm, 這檔案有以下內容:

<html> <body>

<script>
function
checkIt(entry)
{ if ( (
entry.value > 19) && (entry.value < 41) )
    { alert ( "
OK" )      }
  else { alert ( "
數值不正確" )  }
}
</script>

<form name=fm1> 請在兩個文字框輸入 20 至 40 之間的數值: <p>
<input type=text name=
tx1 onChange=" checkIt (this)">
</form>

<form name=
fm2>
<input type=text name=
tx2 onChange=" checkIt (this)">
<p>
</form>

</body> </html>

2. 網頁中有兩個文字框, 請你在文字框放下 20 至 40 這範圍內的數字, 放下數字後, 用滑鼠在文字框範圍外按一下, 看有什麼反應。然後再輸入這範圍外的數字, 看有什麼反應

 

1. 這例子有兩個 this, 請首先看第一個文字框的 this :

<form name=fm1> 請在兩個文字框輸入 20 至 40 之間的數值: <p>
<input type=text name=
tx1 onChange=" checkIt (this)">
</form>

  這一個 this 代表了這目前的物件, 即是 document.fm1.tx1, onChange 表示觀看者改變文字框的內容, 就會啟動 checkIt( ) 這一個 function, 同時將 this 作為 argument, 因此在 checkIt(entry) 這 function, entry 等於 this, if statement 中有以下的代入資料:

    if ( (entry.value > 19) && (entry.value < 41) )
       ↓          ↓
    if ( (this.value >
19) && (this.value < 41) )
       ↓           ↓
if( (document.
fm1.tx1.value > 19) && (document.fm1.tx1.value < 41) )

 

2. 第二個文字框也有一個 this , 這個 this 代表 document.fm2.tx2, 所以會將 document.fm2.tx2 這 argument 傳給 checkIt(entry)

 

3. 從這例子, 你可知道一個網頁內可以有多個 this, 每個 this 在某個地方就代表該地方的物件。

 

 

6. typeof operator

  typeof 是用來傳回一個 operand (運算元) 的類別, 有以下語法:

typeof operand   或   typeof (operand)

  這處的 operand 可放在 ( ) 內, 也可略去這括號。

 

練習-56 typeof 的使用

  請你開啟示範磁碟中的 typeof.htm, 這網頁有以下內容:

<html> <body>
<script>
function
checkIt( ) { }
a=10
b=Math.PI
//   這是一些用作舉例的 function, variable 及 object
c="hello"
nameList
=new Array("Peter","Paul")


document.write(typeof a + "<br>" )
document.write(typeof
b + "<br>" )
document.write(typeof
c + "<br>" )
document.write(typeof
checkIt + "<br>" )
document.write(typeof parseInt + "<br>")
document.write(typeof
nameList +"<br>")
document.write(typeof window + "<br>" )
document.write(typeof true + "<br>" )
document.write(typeof false + "<br>" )
document.write(typeof onBlur + "<br>" )
</script>
</body> </html>

  請你留意用 typeof 傳回的類別名稱。

  typeof 在一般的 JavaScript 很少要到, 這主要是讓程式員用來查看一些名稱是屬於哪一個類別。

 

 

7. void operator

  void 是使一個 expression 不傳回數值, 語法是 void(expression) ,請先看以下 script, 這會在 alert 對話盒傳回一個 true 字。

<script>
A= (10 > 5)
// 這使到 A 等於 true
alert(" A is: " + A )
</script>

 

  若用以下的 script, 這會在 alert 對話盒傳回 undefined 這字, void (10>5) 是不傳回任何數值。

<script>
A= void (10 > 5)
alert("
A is: " + A )
</script>

 

  void 也可用於 URL 來作為一個虛擬的位址, 引來這處的位址沒有任何反應, 瀏覽器不會發出找不到位址的訊息, 請參看 練習-86 的例子。若使用 http://www.null.comhttp://www.nowhere.com 這些假位址, 就會有提示錯誤的訊息。

 

  請你先造出以下網頁, test.nowhere.com 是一個虛構的位址, 網頁開啟後, 請你試用滑鼠指標按網頁上的連結, 看有什麼錯誤的訊息。

<html><body>
<a href=
http://www.test.nowhere.com> Test this link </a>
</body> <html>

  請你再試試以下網頁, 用滑鼠指標按網頁上的連結, 看有什麼反應。

<html><body>
<a href=javascript:void(0)>
Test this link </a>
</body> <html>

 

  在這例子, javascript: 這個字是 URL 的操作指示, 就如 http: 是表示隨後的是 WWW 傳送模式, mailto: 是電郵傳送模式, javascript: 則表示隨後的是一個 JavaScript 語法, 例如 void(expression), 這處的例子沒有expression, 所以就變為 javascript:void(0)

 

 

 


7.9 Operators 的優先處理次序

  在數學上, 你應知道有"先乘除、後加減"這個規則, 例如:

            3 + 4 / 2 = 5

  這運算式有 +/ 兩項運算, 先執行的會是乘除, 後到加減, 若有一些運算要優先處理, 可放在 ( ) 內, 例如: (3+4) / 2 = 3.5。

  在前面說的多個 operators, 當一個 statement 內有多個 operators, 這些 operators 也有一個預設的優先次序 (precedence), 下表是這些 operators 的優先次序, 若要一些低優先次序的 operators 先執行, 可將這些 operators 放在括號內, 不過良好的編寫程式習慣是無論一個 operator 是高或低優先次序, 我們都應將要先執行的部份放在括號內, 例如 3 + (4 / 2) = 5 , 這就安全得多。

  Operators   
 ++ -- - ~ ! (Unary)  
 * / % (Multiplicative)       ↑ 
 + - (Additive)  高
 << >> (Shift)  優
 < > <= >= (Inequality)  先
 == != (Equality)  
 & (Bitwise AND)  
 ^ (Bitwise XOR)  低
 | (Bitwise OR)  優
 && (Logical AND)  先
 || (Logical OR)  ↓
 = (Assignment)  

 


( 第 7 章 完 )