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

181
Visualizações
JS: If you call the function, the result is 3, but if you bind the context with f.call(f) - the result is 5
var f = function() {
  this.x = 5;
  (function() {
    this.x = 3;
  })();
  console.log(this.x);
};

var obj = {
  x: 4,
  m: function() {
    console.log(this.x);
  }
};

f(); // 3
new f(); // 5
f.call(f); // 5
obj.m.call(f); // 5

If you call the function, the result is 3, but if you bind the context with f.call(f) - the result is 5. Also, if I call the functions as a constructor. Why it happens? Sorry for the stupid question, but I tried to find the answer, but could not.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

  • In the first case, you call f() as a simple named function. So the this keyword inside of it refers to the global scope (the window).

    Consequence: the this inside the function body and the this inside the closure inside the function body point to the same object: the window.

  • In the second case you call f() as a constructor, so the this keyword will point to the created object. But the closure, as it's anonymous, will keep refering to the global scope with this, so in this case they don't point to the same thing and the object created by the constructor f() is not mutated by the closure.

  • In the 3rd case, you call f like the first time but specifying the value of this as being the function f itself. So the behavior of this here is equivalent to the second case.

  • In the 4th case, you call m using the context of f, so the this.x will refer to f for this, and the closure inside f keeps refering to the window.

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