0

I have the following code. I want to use the prototype keyword as I want to call method functions and not class methods, why does this give me an error? If I remove the prototype call this works. How can I write this code so I'm able to use instances not class methods?

//app.js
var MyTest = require('./MyTest')
var myTestInstance = new MyTest()
myTestInstance.testFunction(function(reply){
   console.log(reply)
})

//MyTest.js
module.exports = function() {

   function MyTest() {}

   MyTest.prototype.testFunction = function(cb) {
      cb('hello')
   }

   return MyTest

}
1
  • @JohnnyHK myTestInstance.testFunction(function(reply){ ^ TypeError: undefined is not a function at Object.<anonymous> Commented Jul 19, 2015 at 17:26

1 Answer 1

2

to have your app.js working as it is you need to replace the content of MyTest.js with the following:

function MyTest() {}
MyTest.prototype.testFunction = function(cb) {
  cb('hello');
};
module.exports = MyTest;

As you have it in app.js you need a constructor and not a factory function.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! That actually helped alot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.