0

whats wrong with my below code? Im getting error Object # has no method 'subtract'.

function result() {

}
result.prototype.add = function (a,b) {
var sub = this.subtract(a,b);
}
result.prototype.subtract = function (a,b) {
return a-b;
}

module.exports = result;
user87267867
  • 1,409
  • 3
  • 18
  • 25
  • 1
    You need to also share the code that's actually causing the error – Jani Hartikainen Aug 16 '13 at 11:45
  • i have posted the code above which is causing error. – user87267867 Aug 16 '13 at 11:53
  • 1
    That code doesn't fire any error (after you define module as a dumb object to support last line). I guess the error is fired in some other part of your code that you haven't posted – ejosafat Aug 16 '13 at 12:10
  • @user87267867: You need to show us how you create a `result` instance and how you invoke the `add` method (which you seem to do wrong). Read on the [`this` keyword](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this) – Bergi Aug 16 '13 at 13:58
  • @Bergi result.prototype = new Validator(); function result() { } result.prototype.validate = function (a,b) { if ((a !=0) && (b !=0)) { return this.add(a,b); } } – user87267867 Aug 19 '13 at 03:47
  • Thanks, using [`new Validator` for the prototype might be your problem](http://stackoverflow.com/questions/12592913/what-is-the-reason-to-use-the-new-keyword-here). Still you didn't show us *how your create a `result` **instance** and how you invoke the `add` method*! – Bergi Aug 19 '13 at 14:40

1 Answers1

0

A quick guess would be to call a new function.

function result() {

}
result.prototype.add = function (a,b) {
   var sub = this.subtract(a,b);
}
result.prototype.subtract = function (a,b) {
   return a-b;
}

module.exports = new result();
iConnor
  • 19,997
  • 14
  • 62
  • 97
  • Well, then it's probably the way you're calling the function that's the problem, how are you using this? – iConnor Aug 16 '13 at 11:55