I have worked quite a lot with javascript but yesterday, I started using node.js. It's a little script that runs jslint on the files of a folder. For this example, I changed the command to call ls
instead of jslint
.
var sys = require("sys");
var fs = require("fs");
var cp = require('child_process');
var path = fs.realpathSync("./src/");
fs.readdir(fs.realpathSync("./src/"), function(err, files) {
for (var i = 0; i < files.length; i++) {
var filename = files[i];
var complete = path + filename;
// Run jslint on each file
var jslint = cp.exec("ls " + complete, function(error, stdout, stderr) {
console.log(filename + " : " + stdout);
});
}
});
The output is this :
jskata.nofreeze.js : /home/dan/php/jskata/src/jskata.undo.js
jskata.nofreeze.js : /home/dan/php/jskata/src/jskata.nofreeze.js
jskata.nofreeze.js : /home/dan/php/jskata/src/jskata.timezone.js
Why do the line console.log(filename + " : " + stdout);
always prints jskata.nofreeze.js
when the filename should obviously match the result of the ls
? Are closures and scopes different in node.js than in javascript?