2017-08-30 34 views
1

如果當我運行作爲編寫的代碼語句不提醒可變

<html> 
 
<head> 
 
<title></title> 
 
<script src="https:/code.jquery.com/jquery-3.2.1.js"></script> 
 
</head> 
 
<body> 
 
    <button>Button</button> 
 
    <script> 
 
     var user_response=''; 
 
     $('button').on('click',ask_a_question); 
 

 
     function ask_a_question(){ 
 

 
      user_response = prompt('What is your item name?').toLowerCase(); 
 

 
      if (user_response === 'apple') 
 

 
//Between here is where i dont understand why, 
 
//when the if statement is true, that it does not "alert" the bucketlist 
 
//variable and then tells the user **"'learn to juggle', 'take a falconry 
 
//class', 'climb mt everest'."** Does this code even make sense? 
 

 
      alert(bucketList); 
 

 
      var bucketList = [ 
 
       'learn to juggle', 
 
       'take a falconry class', 
 
       'climb Mt Everest' 
 
      ]; 
 

 
     } 
 
    </script> 
 
</body> 
 
</html>

+1

嘗試它的定義之前,提醒的東西是徒勞 – adeneo

+0

把'alert'後'bucketList'。警報本質上是阻塞的。所以'bucketList'只有在你接受alert的時候才被定義,這就是爲什麼你得到undefined – Ionut

+0

雖然javascript中的var被掛起,但是賦值並不是。就像所有人都說的,在定義陣列之前提醒您。 – Keith

回答

4

您正試圖在定義它們之前顯示bucketList的值。

嘗試移動

var bucketList = [ 
    'learn to juggle', 
    'take a falconry class', 
    'climb Mt Everest' 
]; 

上述alert(bucketList);

+1

非常感謝你 –

1

,我得到了警告,指出「未定義」。

如果我將警報移至定義bucketlist的位置之後,我會看到顯示陣列內容的警報。

相關問題