问题很奇怪:我通过拼接函数从数组(关联)中删除了一些元素,之后我添加了一些元素,突然出现了“欠精细”的元素。下面是日志(在“//”之后这是我对此的评论):
// adding first element; array length = 1
["asdasdasd"] microblog.js:48
// adding second element; array length = 2
["asdasdasd", "asdasdasd"] microblog.js:48
// adding third element; array length = 3
["asdasdasd", "asdasdasd", "asdasdasdqwe"] microblog.js:48
// removing second element; array length = 2
["asdasdasd", "asdasdasdqwe"] microblog.js:66
// removing third element; array length = 1
["asdasdasdqwe"] microblog.js:66
// adding new element; array length = 4
["asdasdasdqwe", undefined × 2, "asdasdqwrevcvxzvvxwfrqeqwewq"]
这就是我添加元素的方式:
(...)
this.list[currId] = text;
(...)
这就是我删除元素的方式
(...)
this.list.splice(currId, 1);
(...)
如何避免这种未定义的元素?