I'm a novice to Javascript, and experienced programmer, and i'm struggling real hard with grasping the concept in pattern and design of the best structure to use and approach while building applications. A few areas are quite new to me, one being mastering the scopes and closure of Javascript.
So to make this as clean as possible for you guys i've attached a sample of code to this post, hoping that someone may put my logic to place and give me a guideline to re-think and think right.
The code that i'm posting is part of a very simple file system / helper library (Node.js, express) that is used to scan directories, run a few conditions on those directories and return the result as a map in dimensions of an array to another library (not posted). The code you see is my current "testing" condition. I've commented it fairly.
The gist: https://gist.github.com/jimmiehansson/6235613 (FULL VIEW OR CODE / LIBRARY)
Sample code block (with problem in returning values)
ApiFS.prototype.scan = function(){
var self = this;
var b;
fs.readdir(stgs.fsPath, function(err,folders){
if(folders.length<1) { console.log(errLog.fsEmpty); return; }
rslt = self.walk(folders);
rslt.forEach(function(itm){
if(!self.exclusion(rslt)){ b=self.loader(stgs.fsPath,itm); }
});
}, ioResponse);
console.log(b); // undefined <----
}
So enough icebreaking. My biggest problem:
No matter what I do, the scope (hoisting) and closures which are fairly small, get in my way. I use no getter or setter currently, because I simply don't know how I should? Is my logic completely off, or am I missing something rather fundamental?
In the code you will see that the scan() prototype function makes a single call to each function "helper" that return either true/false value or an array.
I can work with these simply by returning values to the main scan() function, however I cannot for the sake of my life get those values from the fs.readdir() function to return outside of the scope, for instance outside of ioResponse callback.
What is my logic missing, how can I better approach this (even if it means reconstructing every thing) I am worried that I am doing something terribly wrong in my approach to design and layout these functions, and how i handle the values in between them.
Any assistance is greatly appreciated!