【问题标题】:updating the outer scope object in side self-invoking function?更新侧自调用函数中的外部范围对象?
【发布时间】:2015-01-27 09:30:36
【问题描述】:

谁能帮我理解我在做什么错误? 或者帮我提供一个替代解决方案?

http://jsfiddle.net/mrrajesh1982/eynpu8rj/2/

  function isValidImageUrl(url, callback) {
    $('<img>', {
        src: url,
        load: function() {
            callback(true);
        },
        error: function() {
            callback(false);
        }
    });
}
var ndata = [{imgUrl: "https://library.barnard.edu/sites/default/files/paired_t_test_output_1_and_2.png"},{imgUrl: "url1"},{imgUrl: "http://statistics-help-for-students.com/How_do_I_report_paired_samples_T_test_data_in_APA_style_files/image002.jpg"}];


for (var i = 0; i < ndata.length; i++){
    var imageUrl = ndata[i].imgUrl;
    (function(pictureUrl){
        isValidImageUrl(pictureUrl, function(result) {
            if (!result) {
                ndata[i].imgUrl = "http://media.tcc.fl.edu/webcourses/ctll/Developing_Your_Teaching_Philosophy/examples.jpg";
                //I am getting error on this line as TypeError: ndata[i] is undefined

                //My question here is to how to update the ndata array obejct with valid image URL? 
            }
        });
    }(imageUrl));
}
console.log(JSON.stringify(ndata));// Expecting ndata[1].imgUrl should get updated inside self invoking function with valid image url

【问题讨论】:

  • i 错误,您在对象更新之前登录。
  • @ Scimonster ,由于我已经制作了自调用功能,我希望它应该可以正常工作。你有可能完成这项工作吗?我会为你的帮助感到遗憾。我了解回调逻辑,但不知道如何在 for 循环中使用它?

标签: javascript closures self-invoking-function


【解决方案1】:
  function isValidImageUrl(url, callback) {
    $('<img>', {
        src: url,
        load: function() {
            callback(true);
        },
        error: function() {
            callback(false);
        }
    });
}
var ndata = [{imgUrl: "https://library.barnard.edu/sites/default/files/paired_t_test_output_1_and_2.png"},{imgUrl: "url1"},{imgUrl: "http://statistics-help-for-students.com/How_do_I_report_paired_samples_T_test_data_in_APA_style_files/image002.jpg"}];


for (var i = 0; i < ndata.length; i++){
    var imageUrl = ndata[i].imgUrl;
    (function(pictureUrl, i){
        isValidImageUrl(pictureUrl, function(result) {
            if (!result) {
                ndata[i].imgUrl = "http://media.tcc.fl.edu/webcourses/ctll/Developing_Your_Teaching_Philosophy/examples.jpg";
                //I am getting error on this line as TypeError: ndata[i] is undefined

                //My question here is to how to update the ndata array obejct with valid image URL? 

            console.log(ndata);
            }
        });
    }(imageUrl, i));
}

除了传递imageUrl,您还需要将i 传递给您的匿名函数,否则在回调运行时它已经失去了它的值。

另一件事,您在回调运行之前运行console.log()。您需要从回调中登录。另请注意,不能保证它会按顺序返回。

【讨论】:

  • 感谢@Scimonster 的快速回复。我的用例略有不同,我从 ajax 调用中获取 JSON 对象数组(ndata)作为响应。收到回复后,我需要验证每个对象并在必要时更新图像 url。然后通过下划线客户端模板将更新的响应传递给 UI。这就是我想在 for 循环之外更新 ndata 的原因。请帮助我如何使这成为可能?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-15
  • 2020-11-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多