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

376
Vistas
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 Respuestas
Responde la pregunta

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