例如,以下程式 a 為變數, b 為常數
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | function run() { var c = document.getElementById( "content" ); var n = document.createElement( "p" ); var a = 22; const b = 23; a++; b++; n.appendChild(document.createTextNode(a + b)); c.appendChild(n); } /* 《程式語言教學誌》的範例程式 檔名:run36.js 功能:示範 JavaScript 程式 作者:張凱慶 時間:西元 2010 年 11 月 */ |
利用以下的 HTML 文件載入
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | < html > < head > < title >JavaScript Demo</ title > < script src = "run36.js" type = "text/javascript" ></ script > </ head > < body > < input id = "b" type = "button" value = "RUN" onclick = "run();" > < div id = "content" ></ div > </ body > </ html > <!-- 《程式語言教學誌》的範例程式 檔名:jsexample39.html 功能:示範 JavaScript 程式 作者:張凱慶 時間:西元 2010 年 11 月 --> |
瀏覽器 (broswer) 開啟後執行,如下

雖然變數 a 遞增後變成 23 ,常數 b 由於是常數,所以運用遞增運算子其值仍維持 23 ,因此顯示的結果為 46 。
中英文術語對照 | |
---|---|
關鍵字 | keyword |
變數 | variable |
常數 | constant |
瀏覽器 | broswer |
沒有留言:
張貼留言