I'm writing a script that enables me to create an object that uploads files and that should have methods to get properties such as the total size of the files uploaded, let me show you:
function FileUploader = function (to){
let html = "some html with the form...";
to.append(html);
let input = 'someSelector...';
let allSizes = [];
let allSrcs = [];
let totalSize = 0;
input.on('change', function (){
//some validation
let data = new FormData();
//for loop to append the input files
let request = createRequest(); //createRequest returns a request...
request.addEventListener("load", function (evt){
for( let i = 0; i<input.files.length; i++){
totalSize+= input.files[i].size;
allSizes.push(input.files[i].size);
}
let response = JSON.parse(e.currentTarget.responseText);
for(let i = 0; i<response.length; i++){
allSrcs.push(response[i]);
}
alert("Ok");
);
//other request handlers
request.open("POST", 'someLocalPath');
request.send(data);
});
}
Uploader.prototype.getSrcs= function (){
//the problem comes here, I can't pass the value of any variable in the object
}
I have tried transforming the let variables into this
assignments like this:
function FileUploader = function (to){
let html = "some html with the form...";
to.append(html);
let input = 'someSelector...';
this.allSizes = [];
this.allSrcs = [];
this.totalSize = 0;
input.on('change', function (){
//some validation
let request = createRequest(); //createRequest returns a request...
request.addEventListener("load", function (evt){
for( let i = 0; i<input.files.length; i++){
this.totalSize+= input.files[i].size;
this.allSizes.push(input.files[i].size);
}
let response = JSON.parse(e.currentTarget.responseText);
for(let i = 0; i<response.length; i++){
this.allSrcs.push(response[i]);
}
alert("Ok");
);
});
}
In the last case I get undefined errors such as cannot read property push of undefined.
I really hope you can help me, thank you a lot!