Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

107
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda