Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

101
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda