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

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

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 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