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

375
Visualizações
javascript why calling a method from another method in the same class need this?

JavaScript code below.

class Test{
    testMethod1() {
      console.log('hello world');
    }
    testMethod2() {
      this.testMethod1();
    }
}
let t = new Test();
t.testMethod1();
t.testMethod2();

I already know that when calling a method from another method in the same class needs a this pointer. But why JavaScript need this? Seems that cpp, java and othter object oriented langauge do not need this.

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

0

this in this context means you refer to the current object which is just instantiated by new. Without this, the compiler will understand that you refer to an outer-scoped function.

function testMethod1() {
   console.log('outer world');
}

class Test{
    testMethod1() {
      console.log('hello world');
    }
    testMethod2() {
      testMethod1(); //removed `this`
    }
}

let t = new Test();
t.testMethod1(); //hellow world
t.testMethod2(); //outer world

I'm not sure where you are pointing out that Java or other object-oriented programming languages do not need this. You can try this playground for Java.

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