2017-09-20 132 views
0

我想通過一個整數值我回調,這是不按預期工作,因爲可用作爲參考。如何將值傳遞給JavaScript回調/ clousure作爲傳值

for (var i = this.texturesPath.length - 1; i >= 0; i--) { 

var textureIndex = i; 


loader.load(baseTexturePath + this.texturesPath[i], function(texture) { 

    scope.textures[textureIndex] = texture; 
}); 

這種情況下的解決方案/方法是什麼?

回答

-1

這就是你需要的一個IIFE,所以你可以「保存」我;

for (var i = this.texturesPath.length - 1; i >= 0; i--) { 
    (function (index){ 
    loader.load(baseTexturePath + this.texturesPath[index], function(texture){ 
     scope.textures[index] = texture; 
    }) 
    })(i) 
} 
+0

@ user3804449爲什麼你投我的答案,我相信是一個正確的答案 – AngelSalazar