Javascript, global use of this in class



  • Let's say there's a code

    class MyClass{
    
    constructor(files){
        this.files = files;
    }
    
    moveUploadedFiles (params,cb){
        this.createFolder(params.dest,function(){
            async.each(this.files, function(file, next) {  //error here, this.files = undefined
                
                //..
            }
        }
                       
    }
    
    createFolder (dir,cb){
        
    }
    
    //....
    

    }

    Why, inside async.eachthis.files = undefined?

    How to do everything that is declared to be in the design of a global inner class, even if it is used in the methods used?

    p.s. If ES6 class was not used, a common approach could be used, this Example such as: var $this = thisand this has solved all the problems (as is done with this example). How do we use the new ES standard?



  • How do we use the new ES standard?

    For example, replace

    async.each(this.files, function(file, next) { 
    

    async.each(this.files, (file, next) => { 
    



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2