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

338
Vistas
What is the difference between Arrow Functions and Regular Functions when used for Class methods?

When working with Javascript objects, there is a big difference between using Arrow Functions compared to Regular Functions when it comes to this. For instance using a regular function here will properly bind this:

const person = {
  name: "Karen",
  age: 40,
  hi: function() {
    console.log("Hi " + this.name);
  }
}

person.hi() // "Hi Karen"

compared to using an Arrow Function which will incorrectly bind this:

const person = {
  name: "Karen",
  age: 40,
  hi: () => {
    console.log("Hi " + this.name);
  }
}

person.hi() // "Hi "

However, I am not sure if this difference applies when defining class methods. For instance, if I declare two methods - one as arrow function, one as regular function - they both seem to correctly bind this.

class Example {
  constructor() {
    this.values = [1, 2, 3, 4]
  }
  
  arrowMethod = (num) => {
    console.log(this.values.map((item) => item * num));
  }
    
  method(num) {
    console.log(this.values.map((item) => item * num));
  }
}

const example = new Example();
example.arrowMethod(2); // [2, 4, 6, 8]
example.method(2); // [2, 4, 6, 8]

So is there any reason to use one or the other when it comes to class methods?

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