作者SheueJi (雪姬)
看板Web_Design
標題[問題] JavaScript的if..
時間Wed Aug 21 15:01:32 2013
我在看書的時候看到下面的程式範例
function touchRock() {
var userName = prompt ("What is your name?", "Enter your name here.");
if (userName) {
alert("It is good to meet you," + userName +".");
document.getElementById("rockImg").src = "rock_happy.png";
}
}
想請教,在if的部份是表示已經假定有獲得userName的訊息了嗎?
對if這部份不太了解,它是不是可以解釋成" if get "?
請多指教 謝謝大家喔
--
※ 編輯: SheueJi 來自: 36.224.219.229 (08/21 15:02)
→ bibo9901:是, 因為執行完 prompt 才會繼續下一行 08/21 15:08
推 tooto1985:if裡面變數為"",null,undefined皆為false其餘為true 08/21 16:10
推 mrbigmouth:prompt是同步的 在使用者回應前都不會執行其他程式碼 08/22 09:01
→ mrbigmouth:所以if userName時一定已經取得prompt的結果了 08/22 09:01
→ mrbigmouth:如果使用者按取消或空白 那userName就會是undefined等 08/22 09:02
→ mrbigmouth:在if被判定為false的值 08/22 09:02
推 aspdoctor:我都念成如果有這個值 08/23 13:03