tags:

views:

66

answers:

2
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?

+2  A: 

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: http://stackoverflow.com/questions/710542/jquery-javascript-this-pointer-confusion

Simon Groenewolt
+1  A: 

Hi,

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();
}

Cheers

Ramesh Vel

Ramesh Vel