我的代码:
var textureMap = {};
var map = {"a" : 1, "b" : 2, "c" : 3};
for ( var key in map) {
var url = "images/" + key+ ".png";
var texture = THREE.ImageUtils.loadTexture(url, {}, function() {
textureMap[key] = texture;
console.log(key); // becomes c all times
});
}
我如何能够将密钥作为参数发送到回调函数中?因此我用3个纹理填充textureMap,而不是在同一个键上覆盖它。可能是一个非常基本的JavaScript问题,我很感激快速回答!
由于
已解决以这种方式很快:
var textureMap = {};
var map = {"a" : 1, "b" : 2, "c" : 3};
for ( var key in map) {
var url = "images/" + key+ ".png";
var texture = THREE.ImageUtils.loadTexture(url, {}, (function(key) {
textureMap[key] = texture;
console.log(key); // outputs, a,b,c
})(key));
}