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

178
Vistas
Explain the running result of this Javascript code?

const obj = {
    length:10,
    log() {
       console.log(this.length)
    }
}
obj.log() // 10

const length = 20
const fn = obj.log
fn() // 0

const arr = [30,obj.log]
arr[1]() // 2

Why the fn() result is 0 ? if use var length = 20 instead of const length = 20 the result is 20, how this happening?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

the differences lies in what this means in the context of execution of the function log

in the first case this = obj so this.lenght = 10

in the second case this is the window object in the browser so if you use var or if you write window.length it returns the value you set

in the third case this means the array so it returns the array length

const obj = {
    length:10,
    log() {
       console.log(this.length)
    }
}
obj.log() // 10

const length = 20
const fn = obj.log
fn() // 0
window.length = 20
fn() // 20

const arr = [30,obj.log]
arr[1]() // 2

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