Recently I have been busy with nodejs and I thought to create something like this Java input library, so I started with some rough code like below:

'use strict';
const fs = require('fs');

class StdIn {
constructor(opt_name) {
this.name = opt_name;
this.rs = this.name ? fs.createReadStream(this.name) : process.stdin
}

this.rs.on('data', (chunk) => {
let line = chunk.toString();
let numbers = line.trim().split(/\s/);
numbers.forEach(num => {
callback.call(this, num);
});
});
this.rs.on('end', () => {
process.exit(0);
});
}

this.rs.on('data', (chunk) => {
let line = chunk.toString();
callback.call(this, line);
});
this.rs.on('end', () => {
process.exit(0);
});
}
}

const stdIn = new StdIn(process.argv[2]);

//  console.log(num);
//});

console.log(line);
});


Its working fine till now and buts its still incomplete and the code is too messy, the problem clearly visible here that I am trying to push event oriented code into my object oriented world, I am confused basically for two things:

1. It is practical to do so?
2. If yes then what would be the best way?

... the problem clearly visible here that I am trying to push event oriented code into my object oriented world

There is nothing wrong with object oriented and event oriented code being intermingled. It's like saying "toast and jam should not be used in the same breakfast." Not only can you mix the two, depending on your needs you should mix the two. The "problem" I think you're referring to is mixing object oriented with functional programming.

Since this is JavaScript, and it handles both programming paradigms good enough, feel free to mix the two. The only thing to consider is "callback Hell" where you have callbacks nested inside callbacks. For this, the Promise Pattern was created.

On to my favorite subject, naming things.

• In the StdIn constructor, the argument is called opt_name, yet everything else appears to be written in camelCase. Pick either snake_case or camelCase and be consistent. To be consistent with other API's I would recommend camelCase

• The opt_name argument could just be called name since there is not ambiguous reference between the argument and the property on the StdIn object

• Rename the rs property to something meaningful, like: