Javascript回调函数范围问题

时间:2013-05-02 20:04:07

标签: javascript callback scope

我遇到了麻烦

How to assign value in the callback function?

感谢罗迪克,我对如何做到这一点有了一般的了解。但是,我仍然不确定如何将img值传递给getImage function ......

project.prototype.getImagePath = function(){

   codes...

   this.getFolder(path);

   //how do I get the img value inside my getImage function?
}


project.prototype.getFolder = function(path){

this.imagePath;
var instance = this;
var images = ['http://project/path.png', 'http://project2/path.png'];

   function getImage(images, callback) {
        var iterator = function (i) {
           if (i < images.length) {
                     var image = newImage();
                     //..codes to check if image path is valid
                     //if valid, pass image back
                        callback(image);

                    }

                    iterator(++i);
          }
        }
        iterator(0);
    }


   getImage(images, function (img) {
       console.log(img)  //output the img 
       instance.imagePath = img
   });

}

感谢帮助伙伴~~

0 个答案:

没有答案