function MyClass(projectName) { this.projectName = projectName; } MyClass.prototype.createHttpRequestObject = function() { /* do something */ } MyClass.prototype.submit = function(message, script, line) { httpRequest = this.createHttpRequestObject(); }
错误'this.createHttpRequestObject不是函数'来自'httpRequest = this.createHttpRequestObject();'。为什么?我做错了什么?
答案 0 :(得分:2)
JavaScript解释'this'的方式与您预期的不同。它不会链接到“原始”对象,而是链接到当前上下文。
答案 1 :(得分:1)
如果你正确地实例化MyClass它应该工作..看看下面的工作代码..
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();
}