Q:

Object orientation issue on NodeJS

I am having a hard-time in object orientation. Take a look at the following code:
SomeClass = function(){
        this.sanityCheck = 0;

        this.createServer = function(){
            console.log('creating server');
            require('http').createServer(
                this.onRequest
            ).listen(
                8080
            );
            console.log('server created');
        }

        this.onRequest = function(req , res){
            console.log('request made');
            res.writeHead( 200 , {'content-type' : 'text/plain'} );
            var d = new Date();
            res.write('Hello World! \n' + d.toString() + '\n');
            console.warn( this.sanityCheck ); // <!> MY ISSUE
            res.end();
            console.log('response sent');
        }
};

var obj1 = new SomeClass();
obj1.createServer();

The line  console.warn( this.sanityCheck );  shows undefined on console. How do I get the  obj1  inside the  this.onRequest  function (the original, not a copy)?
Thanks a bunch in advance.

A:

Http.createServer has no idea about your object... so you must bind the method to it before sending it:
createServer(
                this.onRequest.bind( this )
            )

Unrelated tip: Instead of piling up on indentation, you can move your methods outside on the prototype.

