Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

100
Views
Custom Javascript Lib - Access Event and Class Instance

I would like a write a small JavaScript Library.

It is very simple. It is a small Wrapper to Upload Files.

Anyway: I have a problem to access the Instance with in the Event.

I try it like this:

class FileUpload
{
    constructor(element)
    {
        this.element = element;
        
        this.dropDiv = document.createElement("div");
        this.dropDiv.style = "border: 2px solid #007bff; background-color: lightblue; width: 100%;border-radius: 25px;";
        this.dropDiv.className = "text-center";
        this.dropDiv.innerHTML = "Upload<br>File<br>Here";
       
        this.dropDiv.addEventListener("dragover", function(event)
        {
            event.preventDefault();
        });
        
        this.dropDiv.addEventListener("drop", this.dropHandler); 
       
        this.element.appendChild(this.dropDiv);
    }
    
    dropHandler(event)
    {
        event.preventDefault();
        
        this.uploadFile("Test");
    }
    
    uploadFile(file)
    {
        console.log("Logic to Upload the file or whatever...");
        console.log(file);
    }
}

It gets me the error: this.uploadFile is not a function

If i try it like this:

    .....
this.dropDiv.addEventListener("drop", this.dropHandler('Test')); 
       
        this.element.appendChild(this.dropDiv);
    }
    
    dropHandler(testVar)
    {
        console.log(testVar);
        console.log(this);
        
        this.uploadFile("Test");
    }
    
    uploadFile(file)
    {
        console.log("Logic to Upload the file or whatever...");
        console.log(file);
    }

It works. But my problem: i need the eventhandler.

Getting the sender is no problem (this.element / this.dropDiv, ...)

But how do i get the event with the parameters AND the FileUpload instance?

Thank you!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

This seams to work. In the initial code this in the function dropHandler() refereed to the drop element. I changed the two functions to arrow syntax.

In addition(and IMHO more exciting) is how the new Arrow Function binds, or actually DOES NOT bind it’s own this. Arrow Functions lexically bind their context so this actually refers to the originating context. Arrow Functions and Lexical this

So, the function will bind to the object, not the element.

class FileUpload {
  constructor(element) {
    this.element = element;

    this.dropDiv = document.createElement("div");
    this.dropDiv.style = "border: 2px solid #007bff; background-color: lightblue; width: 100%;border-radius: 25px;";
    this.dropDiv.className = "text-center";
    this.dropDiv.innerHTML = "Upload<br>File<br>Here";

    this.dropDiv.addEventListener("dragover", e => e.preventDefault());

    this.dropDiv.addEventListener("drop", this.dropHandler);

    this.element.appendChild(this.dropDiv);
  }

  dropHandler = e => {
    e.preventDefault();
    this.uploadFile("Test");
  }

  uploadFile = file => {
    console.log("Logic to Upload the file or whatever...");
    console.log(file);
  }
}

var f1 = new FileUpload(document.querySelector('div'));
<div>test</div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!