ES6类:方法中的调用方法(XMLHttpRequest)

时间:2017-06-29 12:20:34

标签: javascript class events xmlhttprequest

我班上有以下方法:

triggerSuccess() {
        return function(){
            var DONE = 4;
            var HTTP_OK = 200;
            if (this.readyState === DONE) {

                // This throws an error:
                this.removeEventListener('error', this.triggerError());
                //

                if (this.status === HTTP_OK) {
                    logger.debug('File upload complete');
                } else {
                    var error;
                    if(this.status === 0){
                        error = new Error('ConferenceFileShareUpload: CORS request failed')
                    } else {
                        error = new Error('this.statusText');
                    }
                    error.status = this.status;
                    logger.error('File upload failed: %s', error.message, event);
                }
            }
        };
    }

这是用另一种方法调用的:

upload(file) {
        return new Promise((resolve, reject) => {
            return this.request()

                ...

                }).send().then((result)=>{
                    var uploadUrl = result.message.body.uploadUrl;
                    var xhr = new window.XMLHttpRequest();

                    xhr.addEventListener('load', this.triggerSuccess());

                    ...

                    resolve();
                }).catch((err)=>{
                    reject(err);
            });
        });
    }

我现在在我的triggerSuccess()中得到以下错误:

  

未捕获的TypeError:this.triggerError不是函数

但我在班上有这个:

triggerError(err) {
    this.events.emit(FileShareEvent.error, err);
}

onError(listener) {
    this.emitter.on(FileShareEvent.error, listener);
    return this;
}

0 个答案:

没有答案