【问题标题】:FileReader() - Problems with closure and passing argumentsFileReader() - 闭包和传递参数的问题
【发布时间】:2015-05-30 15:20:54
【问题描述】:

FileReader.onload() 事件有一些问题:

我在以下两个代码 sn-ps 之间得到不同结果的原因是什么?只要我通过 object 实例,而不是显式的 this.attachmentIndex 值,我就会得到不同的结果。

1)

fileReader.onload = (function (file, obj) {
                    return function (evt) {
                        obj.attachmentIndex // 1
                    };
                })(f, this);

2)

        fileReader.onload = (function (file, index) {
            return function (evt) {
                index // 0
            };
        })(f, this.attachmentIndex);

关闭 - FileReader.onload()

我面临的另一个问题是我似乎无法将多个参数传递给我的FileReader.onload() 事件处理程序。

        fileReader.onload = (function (file, one, two) {
            return function (evt) {
                one // 1
                two // undefined
            };
        })(f, 1, 2);

你可以在下面找到整个对象。我不得不做一些丑陋的事情来实现我想要的。我真的希望有更好的方法来达到同样的效果。

function Note() {
    this.attachmentOutput = [];
    this.attachmentIndex = 0;
    this.debugging = false;
    this.currentWorkingObject = new NoteObject(helperFn.createGuid());
    this.addAttachement = function(files) { 
        for (var i = 0, f; f = files[i]; i++) {
            var fileReader = new FileReader(),

            fileReader.onload = (function (file, obj) {
                return function (evt) {
                    var base64File = evt.target.result;
                    obj["note"].addDataToAttachementObj(obj["index"], { data: base64File })
                };
            })(f, { note: this, index: this.attachmentIndex});

            // Async => reading file
            fileReader.readAsDataURL(f);

            this.attachmentIndex++
        }

        var html = app.outputTemplate(this.currentWorkingObject.attachements);
        $('.upload-output').html(html);
    };

};

函数的触发器:

$(".upload-drop-zone").on('drop', function (e) {
        e.preventDefault();
        var files = e.originalEvent.dataTransfer.files;
        app.myCurrentNote.addAttachement(files);
    });

【问题讨论】:

  • 预期结果是什么?
  • 我想要 0 作为闭包中 index 值的结果 - 最后我得到了它,但只是使用了这个丑陋的解决方法。如果我没有明确传递参数,那么我会得到1,我不知道为什么。如果我可以将this 传递给闭包并从那里访问attachmentIndex,那就容易多了。
  • 不确定是否正确解释问题?在for 循环var i = 0, f; f 的目的是什么? i 是否定义为 f = files[i] ?试过for (var i = 0; i < files.length; i++) {
  • 它为我节省了检索文件对象的额外麻烦。我在代码中还需要i 我刚刚跳过了这段代码sn-p,因为我认为它不相关。

标签: javascript closures filereader


【解决方案1】:

我想要 0 作为闭包中索引值的结果 - 最后我 明白了,但只是用这个丑陋的解决方法。如果我没有通过 明确论证,然后我得到 1,我不知道为什么。

FileReader 是异步的。 this.attachmentIndex 似乎在

处增加到1
    // Async => reading file
    fileReader.readAsDataURL(f);

    this.attachmentIndex++ // <- `this.attachmentIndex` incremented to `1` here

可能在调用FileReader.onload 事件之前。

【讨论】:

  • 这就是我的想法,但为什么this + 访问闭包中的索引会给我一个不同的结果,然后将其显式传递为this.attachmentIndex
  • @KingJulien 不确定 “为什么 this + 访问闭包中的索引会给我一个不同的结果,然后将它明确地传递为 this.attachmentIndex 正确?试图从FileReader.onload 事件中增加this.attachmentIndex?例如。;在FileReader.onload 之前定义that = this;,在FileReader.onload 内,在.addDataToAttachementObj 之后,++that.attachmentIndex ?
猜你喜欢
  • 2017-05-25
  • 1970-01-01
  • 1970-01-01
  • 2013-05-31
  • 2017-04-18
  • 2012-02-16
  • 2015-10-13
  • 2017-12-05
相关资源
最近更新 更多