相关疑难解决方法(0)

'this'在JavaScript类方法中未定义

我是JavaScript的新手.我所做的一切都是新的,它是现有代码的调整,并编写了一小部分jQuery.

现在我正在尝试用属性和方法编写一个"类",但是我遇到了这些方法的问题.我的代码:

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
Run Code Online (Sandbox Code Playgroud)

问题是in Request.prototype.start,this是未定义的,因此if语句的计算结果为false.我在这做错了什么?

javascript prototype class

65
推荐指数
6
解决办法
7万
查看次数

标签 统计

class ×1

javascript ×1

prototype ×1