0

I am a bit confused with the code below, can we create class in js this way?

module.exports = function testname(paramas){
  testname.test = function(req, res){
  //some code here
  }
  return testname;
}

should not we use this.test instead of function name.test?

Yalamber
  • 7,360
  • 15
  • 63
  • 89
  • http://stackoverflow.com/questions/5311334/what-is-the-purpose-of-nodejs-module-exports-and-how-do-you-use-it Read this answer for more info about module.exports – Klaasvaak Jan 29 '13 at 12:03
  • If you want to know a bit more about OOP in JS: http://howtonode.org/prototypical-inheritance – Klaasvaak Jan 29 '13 at 12:04
  • What do you actually want to do? Your current code is a function that returns itself and creates a property on itself for each call. – Bergi Jan 29 '13 at 12:04

1 Answers1

0

ClassName.methodname creates static members, while this.methodname creates instance members

function MyClass () {
  this.fn = function () {
    // instance member function
  }
}

MyClass.anotherFn = function () {
  // Static member function
}

If you want to create some variables which should be encapsulated within the class objects when you create them, use this.var.

However, if you want a data member to be shared among all the instances of the same class then use ClassName.var

Salman
  • 9,299
  • 6
  • 40
  • 73