user8348171
user8348171

Reputation:

Javascript return variable value form an object

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!

Upvotes: 0

Views: 67

Answers (1)

user8348171
user8348171

Reputation:

For everyone looking for a quick solution easy to understand here's a solution:

function Person() {
  var that = this;
  that.age = 0;

  setInterval(function growUp() {
    // The callback refers to the `that` variable of which
    // the value is the expected object.
    that.age++;
  }, 1000);
}

That's the most basic example I found. I hope it helps you.

Upvotes: 1

Related Questions