I know this is probably a duplicate but I just can't figure out how to do it (trust me i've searched, i've tried to fiddle around with the code). I have the following code which is building a table of files that you drag and dropped in to the app.
我知道这可能是重复但我无法弄清楚如何做到(相信我,我已经搜索过,我试图摆弄代码)。我有以下代码,它构建一个文件表,您可以将其拖放到应用程序中。
function CreateTrackTable (data, length) {
var fs = require('fs'),
mm = require('musicmetadata'),
i = 0;
for (i; i < length; ++i) {
var mimeType = data.dataTransfer.files[i].type;
if (mimeType == "audio/mp3" || mimeType == "audio/x-m4a"){
FilePath = data.dataTransfer.files[i].path;
var parser = mm(fs.createReadStream(FilePath));
parser.on('metadata', function (result) {
if (result.picture.length == 0) {
$("#track-table").append('<tr path="'+ FilePath +'"><td></td></tr>');
}else{
var picture = base64ArrayBuffer(result.picture[0].data);
$("#track-table").append('<tr path="'+ FilePath +'"><td></td></tr>');
}
});
}
}
}
The Problem is that the FilePath variable is accessible however it "prints-out" always the same path, not the one respected to the loop i.
问题是FilePath变量是可访问的,但它“打印出”总是相同的路径,而不是循环i所遵循的路径。
The app is builded with nodo-webkit everything quite works and this is the only problem I can't figure out.
该应用程序使用nodo-webkit构建,一切都很有效,这是我唯一无法弄清楚的问题。
Thanks for the help!
谢谢您的帮助!
1 个解决方案
#1
3
Each function created inside the loop closes over the same FilePath
(i.e. they don't each get their own copy), which means that they'll each see whatever value that variable has whenever they execute. To make this work how you expect, you need to arrange it so that they do each get their own copy. Since JavaScript variables are scoped to the nearest enclosing function, the way to do that is to wrap the function creation in an immediately-invoked function that receives the desired value as an argument:
在循环内创建的每个函数都关闭同一个FilePath(即它们不会各自得到它们自己的副本),这意味着它们每个都会看到变量执行时所具有的任何值。为了使这项工作符合您的预期,您需要对其进行安排,以便每个人都能获得自己的副本。由于JavaScript变量的作用域是最近的封闭函数,所以这样做的方法是将函数创建包装在一个立即调用的函数中,该函数接收所需的值作为参数:
(function(FilePath) {
parser.on('metadata', function (result) {
if (result.picture.length == 0) {
$("#track-table").append('<tr path="'+ FilePath +'"><td></td></tr>');
} else {
var picture = base64ArrayBuffer(result.picture[0].data);
$("#track-table").append('<tr path="'+ FilePath +'"><td></td></tr>');
}
});
})(FilePath);
In this case, each new function closes over a new FilePath
, which produces the wanted result.
在这种情况下,每个新函数都会关闭一个新的FilePath,它会生成所需的结果。
#1
3
Each function created inside the loop closes over the same FilePath
(i.e. they don't each get their own copy), which means that they'll each see whatever value that variable has whenever they execute. To make this work how you expect, you need to arrange it so that they do each get their own copy. Since JavaScript variables are scoped to the nearest enclosing function, the way to do that is to wrap the function creation in an immediately-invoked function that receives the desired value as an argument:
在循环内创建的每个函数都关闭同一个FilePath(即它们不会各自得到它们自己的副本),这意味着它们每个都会看到变量执行时所具有的任何值。为了使这项工作符合您的预期,您需要对其进行安排,以便每个人都能获得自己的副本。由于JavaScript变量的作用域是最近的封闭函数,所以这样做的方法是将函数创建包装在一个立即调用的函数中,该函数接收所需的值作为参数:
(function(FilePath) {
parser.on('metadata', function (result) {
if (result.picture.length == 0) {
$("#track-table").append('<tr path="'+ FilePath +'"><td></td></tr>');
} else {
var picture = base64ArrayBuffer(result.picture[0].data);
$("#track-table").append('<tr path="'+ FilePath +'"><td></td></tr>');
}
});
})(FilePath);
In this case, each new function closes over a new FilePath
, which produces the wanted result.
在这种情况下,每个新函数都会关闭一个新的FilePath,它会生成所需的结果。