Pattern & Problem Description
This seems way trivial, but never the less I am going to pose because I cannot find a simple clear example. Note if you respond with a function and do not declare a class then I will not accept that as a solution. I do understand that JS considers classes to function...
- I define a class
class MyClass{
constructor(input0){
this._input0 = input0;
this._input1 = [];
}
set_input1(){
/**
* in my case complex algebraic manipulation occurs inside this function
* by putting it inside of a function it encapsulates that
* in this the result is 123 ;)
*/
this._input1.push(123);
}
Now to actually initialize the object as shown here. In this case input0 is set to 'foo'
myobj = new MyClass('foo')
myobj.set_input1()
console.log(myobj._input1) //returns empty array []
TODAY'S EXAMPLE
Today I was building a data aggregator with nodejs and could not get this to go.
ProjDb.js
class ProjDb {
constructor(_rsviewProjDirArr) {
this._rsviewProjDirArr = _rsviewProjDirArr; // Full file directory paths
this._allFileNameArr = [];
} // end constructor
set_allFileNameArr() {
this._rsviewProjDirArr.forEach((i) => {
fs.readdir(i, (err, files) => {
if (err) {
throw (err);
}
else {
files.forEach(file => {
if (path.extname(file) == ".DBF") {
this._allFileNameArr.push(file);
}
});
}
});
});
}
}// end classs = ProjDb
module.exports = ProjDb;
SERVER.js
const XLSX = require('xlsx');
const util = require('util');
const ProjDb = require('./ProjDb.js');
let projDb = new ProjDb(
['./rsview-data/proj1',
'./rsview-data/proj2']);
projDb.set_allFileNameArr();
console.log(projDb._allFileNameArr.length); // result =0 but should be large number
I like this pattern because debugging is very easy on small devices and complex systems. I can break out the problem quickly. It also makes code easy to read. The only thing I have been able to come up with is that because there is a scope wrapping the constructor I cannot redefine the values inside of it...I do this all the time in cpp, and python. So if you can explain from that perspective I would love it. I don't see either of these should fail.
CodePudding user response:
in the constructor you have this._input1 = [];
however the console is console.log(myobj.input1)
works fine if you fix the name typo:
class MyClass{
constructor(input0){
this._input0 = input0;
this._input1 = [];
}
set_input1(){
/**
* in my case complex algebraic manipulation occurs inside this function
* by putting it inside of a function it encapsulates that
* in this the result is 123 ;)
*/
this._input1.push(123);
}
}
myobj = new MyClass('foo')
myobj.set_input1()
console.log(myobj._input1) // [123]
CodePudding user response:
After some digging, I found a way. It appears that you can update the member function as one would expect. The **code in the second example is using node function from fs module fs.readdir. After a review, I found that the files is a keyword for the callback and must be used, additionally it returns an object not an array. So that object must be transformed into a string array using the built-in toString() member.
here is the working copy
set_allFileNameArr() {
// rename i as dirpath
for (let i=0;i<this._rsviewProjDirArr.length;i )
{
let _dirPath = this._rsviewProjDirArr[i];
var logstream = fs.createWriteStream('allFileNameArr.txt', {flas: 'a'});
console.log('Loading Data From Target Directory:\n\t' _dirPath)
// Method 1
// (err,files) are 2 arguments for a callbqack function
fs.readdir(_dirPath, (err, _files)=>{
if (err) throw (err);
else {
// For loop style
for (let i=0;i<_files.length;i ){
if(path.extname(_files[i])==='.DBF'){
this._allFileNameArr.push(_files[i].toString());
logstream.write(_files[i].toString() '\n')
}
}
}
}
);
