我是JavaScript新手。就我所做的所有事情而言,新增了对现有代码的调整,并编写了一小部分jQuery。'this'在JavaScript类方法中未定义
现在我试图写一个“类”的属性和方法,但我遇到了方法的麻烦。我的代码:
function Request(destination, stay_open) {
this.state = "ready";
this.xhr = null;
this.destination = destination;
this.stay_open = stay_open;
this.open = function(data) {
this.xhr = $.ajax({
url: destination,
success: this.handle_response,
error: this.handle_failure,
timeout: 100000000,
data: data,
dataType: 'json',
});
};
/* snip... */
}
Request.prototype.start = function() {
if(this.stay_open == true) {
this.open({msg: 'listen'});
} else {
}
};
//all console.log's omitted
的问题是,在Request.prototype.start
,this
是不确定的,因此,如果语句评估为假。我在这里做错了什么?
你在'prototype'中有'start'的原因吗? – xj9 2010-10-25 03:56:12
什么是'Request.prototype'设置为? – 2010-10-25 03:57:19
我不知道:S – 2010-10-25 04:00:19