readFile
is async method and you lose your execute context
. You need bind your class to readFile callback as a context. Use Function.prototype.bind
:
MyReaderClass.prototype.ReadFiles = function(p_path) {
var files = fs.readdirSync(p_path);
for (var i in files) {
var fileName = files[i];
fs.readFile(path.join(p_path, fileName), 'utf8', function(err, data) {
/*readFile callback*/
}.bind(this));//also here was a syntax error
}
}