-1

So, I'm going through the Node.js learnyounode tutorial (which is pretty fun), but I'm no stranger to javascript. One of the requests is to have the user write a program that accepts a directory and a file extension as arguments, and then proceeds to ls the files which match the given extension.

In checking my arguments in various places where data is passed through functions, I cannot get Node.js to allow me to pass additional arguments to a callback function for a built-in function.

For example, I can run the following snippet, pass an err, path, and extension, and would like to see the all the passed arguments, but I only see err and data in the arguments object while the "ext" variable is ignored.

var fs = require("fs"), path = require("path")
var dir = process.argv[2], ext = process.argv[3];

fs.readdir(dir, function (err, data, ext) {
  console.log(arguments);
  });

The reason for wanting this functionality is to avoid breaking the function scope to retrieve the value of ext.

I can do this type of thing with custom functions all day, but the built-in functions are a little less forgiving.

Thanks, Bob

Bob Smith
  • 145
  • 5
  • Does it make sense to you that "wishing" a function/functionality makes it available? You *know* that's not the API, why are you trying to do that? – Amit May 06 '16 at 23:28
  • I'm not wishing for anything, just wondering if there is a way. I clearly stated why I wanted this functionality, so I'll say it again. "The reason for wanting this functionality is to avoid breaking the function scope" – Bob Smith May 06 '16 at 23:33
  • And I'll repeat as well, "you know that's not the API". – Amit May 06 '16 at 23:36
  • Yes, I know this is not the API, but people come up with clever tricks all the time. – Bob Smith May 06 '16 at 23:40

2 Answers2

0

The easiest way is to just use the built-in path.extname() function on each entry name:

fs.readdir(dir, function(err, entries) {
  if (err) throw err;
  for (var i = 0; i < entries.length; ++i) {
    if (path.extname(entries[i]) === ext)
      console.log(entries[i]);
  }
});
mscdex
  • 104,356
  • 15
  • 192
  • 153
  • Thanks for the solution, but this is not quite what I was asking. – Bob Smith May 07 '16 at 16:36
  • This is really the only way to do it. As mentioned in the comments to your question, there is no way to provide extra function parameters like you were suggesting and have node magically obey and provide values for those extra parameters. Node does not parse callback parameter names or count callback parameters. – mscdex May 07 '16 at 16:57
  • Yeah, extra parameters are out of the question. Looks like the only way to go about it is with custom closures, creating a class to wrap the code in, or let node wrap the module in it's own closure (default behavior). – Bob Smith May 07 '16 at 17:45
0

Talked to the node.js people in irc, turns out the way javascript works such a method to keep scope in bounds is not necessary. Node.js, by default, puts modules in their own closures, so the variables can be accessed outside the function and not interfere with global namespace.

Found this answer on node js and variable scope which probably best matches my situation, but didn't know it's what I needed when I posted my question.

Also found some nice closure examples on Mozilla.

On a personal note I thought I knew javascript a little better than this, and I hope this answer helps people who are coming from C / PHP backgrounds into node.js

Community
  • 1
  • 1
Bob Smith
  • 145
  • 5