2016-11-03 75 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