我有以下代码来制作ajax文件上传请求。但我有多个文件和多个页面要上传,所以想要尝试进入一个我可以重用的对象。
function upload(url,formdata){
var xhr = new XMLHttpRequest();
xhr.open("POST",url);
xhr.upload.onloadstart = function (e) {
//something something....
x='hello';
}
xhr.upload.onprogress = function (e) {
//something something...
y='world';
}
xhr.upload.onloadend = function (e) {
//something something...
}
xhr.send(formdata);
}
foo=new upload("https://example.com",formdata);
现在,我的问题是,如何“传递”或揭露事件为'foo'所以我可以这样做:
foo=new upload(
"https://example.com",formdata
).onloadstart(
//do whatever here, have access to x
).onprogress(
//do whatever here, have access to y
)
我的研究告诉我,我需要使用方法链接,或者更确切地说,这就是所谓的,我需要添加
return this;
每个事件,但就我而言。
感谢任何帮助!