0
function MyClass(projectName) {
  this.projectName = projectName;
}

MyClass.prototype.createHttpRequestObject = function() {
  /* do something */
}

MyClass.prototype.submit = function(message, script, line) {
  httpRequest = this.createHttpRequestObject();
}

The error 'this.createHttpRequestObject is not a function' goes from line 'httpRequest = this.createHttpRequestObject();'. Why? What I do wrong?

Sebastian
  • 2,618
  • 3
  • 25
  • 32

2 Answers2

2

The way JavaScript interprets 'this' is different than you expect. It does not link to the 'original' object but to the current context.

See http://www.quirksmode.org/js/this.html for an explanation.

See also: jQuery/JavaScript "this" pointer confusion

Community
  • 1
  • 1
Simon Groenewolt
  • 10,607
  • 1
  • 36
  • 64
1

it should work if you instantiate the MyClass properly.. take a look at the below working code..

function testCall(){
   var ss = new MyClass("sam");
   ss.submit();
}

function MyClass(projectName) {
  this.projectName = projectName;
}

MyClass.prototype.createHttpRequestObject = function() {
    return "something";
}

MyClass.prototype.submit = function(message, script, line) {
  httpRequest = this.createHttpRequestObject();
}
RameshVel
  • 64,778
  • 30
  • 169
  • 213