遇到一些麻煩與下面的代碼段:未定義陣列/ JavaScript的
I would like to be able to view the contents of $prisonlist
in the console
我想能夠在控制檯查看的$prisonlist
(或$prisonlist[some value]
)中的內容,並將其顯示在div的文本中。 從本質上講,每當我這樣做,它想出了被稱爲「未定義」的錯誤 - 儘管我與它的工作等功能似乎只是正常工作(如//
代碼:
$counter = 0;
var $prisonlist=[];
$prisonlist[0]=1;
$('#entered').text($prisonlist[0]);
$('#s1').click(function(){
$(this).toggleClass('on off');
$counter = $counter + 1;
$('#clicks').text($counter);
$prisn = Math.ceil(Math.random()*10);
$('#prisoners').text($prisn);
$boo=$prisonlist.indexOf($prisn) //finds in array whether there is any
if ($boo==-1){
$prisonlist.push($prisn); // lists the individual inside
}
});
你是否試圖訪問其範圍內的'$ prisonlist'? – Hatchet
我猜這是你的意思超出了它的最大數組長度 - 不,如果我嘗試訪問在那裏定義的'$ prisonlist [0]',它不會顯示任何東西。它會顯示在文本中,如果我改變文本,但我不能查看完整的數組,當各種值已被推入它。 – squeakcode
我認爲@hatchet實際上是在詢問'var $ prisonlist'的定義範圍。它是一個函數的本地函數,它是一個循環還是它在全局範圍內? –