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?

1
  • What's the code that calls MyClass.prototype.submit? Commented Aug 25, 2009 at 9:48

2 Answers 2

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

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

Comments

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

Comments

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.