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

106
Visualizações
This keyword and event handler this keyword

Why does this point inside array.forEach as window object. I understand this is because its a callback function .If so in case of event handlers also where we pass the handler function as a callback function we should be getting this to be the window, but we get the object (i.e. an HTML element) .How are both different?

In below example I have an array method which takes a callback.

var videoPlayer = {
    title: 'Main player',
    tags: ['a', 'b', 'c'],
    getTags() {
        this.tags.forEach(function(item) {
            console.log(this) //window object  
        })
    }
}
videoPlayer.getTags()

PS: I understand the fix and passing the this as a second parameter in the forEach. But I am interested to know the working of this keyword

about 4 years ago · Juan Pablo Isaza
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