可能重复:
Javascript臭名昭著的循环问题?
for (var i=0; i<songList.length; i++){
test.init({
});
}
var test = {
init: function (params) {
var my = this;
var backend = WaveSurfer.Audio;
if (!params.predrawn) {
backend = WaveSurfer.WebAudio;
}
var id = playList.length;
this.id = id;
this.backend = Object.create(backend);
this.backend.init(params);
this.drawer = Object.create(WaveSurfer.Drawer);
this.drawer.init(params);
this.backend.bindUpdate(function () {
my.onAudioProcess();
});
this.bindClick(params.canvas, function (percents) {
my.playAt(percents);
});
playList.push(my);
当我调试这个脚本时:我的第一个元素的 id 是 0;但是当我突然进入第二个循环时,两个 id 都变成了 1。这怎么可能?