H
hukash
Hi everyone,
I lately started playing around with javascript (node.js) and got
stuck avoiding global variables. I hope someone can tell me what I'm
doing wrong.
Here's my script:
---
var sys = require('sys'),
fs = require('fs');
var FileReader = {
directory: null,
result: null,
tempDir:null,
start: function (directory) {
this.directory = directory;
this.tempDir = this.checkDirectory (this.directory);
this.result = this.readDirectory (this.directory);
sys.puts("Directory: " + this.tempDir); // output is undefined
sys.puts("Result: " + this.result); // output is undefined
},
checkDirectory: function(directory) {
fs.stat(directory, function (err, stats) {
if (err) throw err;
if (stats.isDirectory()) {
return directory;
} else {
return false;
}
});
},
readDirectory: function (directory) {
fs.readdir(directory, function (err, files) {
if (err) throw err;
return files;
});
},
};
FileReader.start("/Users");
---
this.tempDir and and this.directory are both undefined. So far I can
tell, the functions checkDirectory and readDirectory are working fine.
How do I assign the results of both functions correctly?
Thanks in advance,
Luke
I lately started playing around with javascript (node.js) and got
stuck avoiding global variables. I hope someone can tell me what I'm
doing wrong.
Here's my script:
---
var sys = require('sys'),
fs = require('fs');
var FileReader = {
directory: null,
result: null,
tempDir:null,
start: function (directory) {
this.directory = directory;
this.tempDir = this.checkDirectory (this.directory);
this.result = this.readDirectory (this.directory);
sys.puts("Directory: " + this.tempDir); // output is undefined
sys.puts("Result: " + this.result); // output is undefined
},
checkDirectory: function(directory) {
fs.stat(directory, function (err, stats) {
if (err) throw err;
if (stats.isDirectory()) {
return directory;
} else {
return false;
}
});
},
readDirectory: function (directory) {
fs.readdir(directory, function (err, files) {
if (err) throw err;
return files;
});
},
};
FileReader.start("/Users");
---
this.tempDir and and this.directory are both undefined. So far I can
tell, the functions checkDirectory and readDirectory are working fine.
How do I assign the results of both functions correctly?
Thanks in advance,
Luke