Javascript 使用FileReader有两个我不懂的功能

Javascript 使用FileReader有两个我不懂的功能,javascript,html,Javascript,Html,我在理解(函数(theFile){return函数(e){如何工作时遇到问题 文件的作用是什么?如果返回e,如何将其用作e.target.result 还有}(f);做什么,为什么它不在函数范围内 // Check for the various File API support. if (window.File && window.FileReader && window.FileList && window.Blob) { // Gre

我在理解
(函数(theFile){return函数(e){
如何工作时遇到问题

文件的作用是什么?如果返回
e
,如何将其用作
e.target.result

还有
}(f);
做什么,为什么它不在函数范围内

// Check for the various File API support.
if (window.File && window.FileReader && window.FileList && window.Blob) {
    // Great success! All the File APIs are supported.
} else {
    alert('The File APIs are not fully supported in this browser.');
}

function handleFileSelect(evt) {
    var files = evt.target.files; // FileList object

    // Loop through the FileList and render image files as thumbnails.
    for (var i = 0, f; f = files[i]; i++) {
        // Only process image files.
        if (!f.type.match('image.*')) {
            continue;
        }

        var reader = new FileReader();

        // Closure to capture the file information.
        reader.onload = (function (theFile) {
            return function (e) {
                // Render thumbnail.
                var span = document.createElement('span');

                span.innerHTML = ['<img class="thumb" src="', e.target.result,
                    '" title="', escape(theFile.name), '" id="', escape(theFile.name), '" onclick="_Element=this.id, RefreshElement()" /><br>'].join('');

                document.getElementById('list').insertBefore(span, null);
            };
        })(f);

        // Read in the image file as a data URL.
        reader.readAsDataURL(f);
    }
}

document.getElementById('files').addEventListener('change', handleFileSelect, false);
//检查各种文件API支持。
if(window.File&&window.FileReader&&window.FileList&&window.Blob){
//非常成功!支持所有文件API。
}否则{
警报('此浏览器不完全支持文件API');
}
功能手柄文件选择(evt){
var files=evt.target.files;//文件列表对象
//循环浏览文件列表并将图像文件渲染为缩略图。
for(var i=0,f;f=files[i];i++){
//仅处理图像文件。
如果(!f.type.match('image.*')){
持续
}
var reader=new FileReader();
//闭包以捕获文件信息。
reader.onload=(函数(文件){
返回函数(e){
//渲染缩略图。
var span=document.createElement('span');
span.innerHTML=['
'].join(''); document.getElementById('list').insertBefore(span,null); }; })(f) ); //作为数据URL读入图像文件。 reader.readAsDataURL(f); } } document.getElementById('files').addEventListener('change',handleFileSelect,false);
函数(文件)
实际上是一个函数闭包。它是一个匿名函数,在事件触发时创建和调用。当reader.onload最终触发时,它返回一个具有正确值的函数。请阅读更多内容。请询问您是否想要一个更详细解释的正式答案。@François Wahl。代码用注释进行了很好的解释,我是被迫的顺便解释一下。这没什么意义,应该是固定的。给我点分数不是很好,因为我给你看了那个bug。还有为什么你要删除你的评论?啊,这就是所谓的闭包。我以前从未见过,也不知道它是什么。我现在研究过闭包,我更了解它。你的链接比2有用你介意我做一个更正式的回答吗?也可以考虑加入和讨论你认为是错误的各种问题(比如你认为你发现的bug)。