作者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)
1F:→ bibo9901:是, 因为执行完 prompt 才会继续下一行 08/21 15:08
3F:推 tooto1985:if里面变数为"",null,undefined皆为false其余为true 08/21 16:10
4F:推 mrbigmouth:prompt是同步的 在使用者回应前都不会执行其他程式码 08/22 09:01
5F:→ mrbigmouth:所以if userName时一定已经取得prompt的结果了 08/22 09:01
6F:→ mrbigmouth:如果使用者按取消或空白 那userName就会是undefined等 08/22 09:02
7F:→ mrbigmouth:在if被判定为false的值 08/22 09:02
8F:推 aspdoctor:我都念成如果有这个值 08/23 13:03