2016-11-03 73 views
0

我想創建一個可以像status[a][b] 這樣的字典,其中ab是在初始化時不知道的隨機字符串。錯誤:狀態[x]未定義

這裏的確切使用情況:

status = {} 
name = {}; 
time = {}; 
score = {}; 
for (i=0; i<$scope.submissions.length; i++) 
{ 
    e = $scope.submissions[i]; 
    status[e.rno] = status[e.rno] || {}; 
    time[e.rno] = time[e.rno] || 0; 
    score[e.rno] = score[e.rno] || 0; 
    status[e.rno][e.problem] = status[e.rno][e.problem] || 0; 
    if (e.score == 100 && status[e.rno][e.problem] == 0) 
    { 
     status[e.rno][e.problem] = 100; 
     time[e.rno] += e.id; 
     score[e.rno] += 100; 
    } 
} 
console.log(score["20161230"]); 

它引發錯誤:Error: status[e.rno] is undefined

回答

1

status是一個窗口,財產,你不能改變它的類型。因此,如果您處於函數作用域(以避免使用全局窗口屬性),則應使用var,或者如果必須是全局的,則使用其他名稱。

+0

我永遠不會發現。謝謝! – anukul

+1

@anukul只要保持總是使用局部變量的習慣,除非你確實需要一個全局變量。 – Barmar