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

91
Vistas
javascript using this in a child method

Method:

a = {
  foo: 1,
  bar() {
    return this.foo + 1
  },
  lol: {
    baz() {
      return this.foo
    }
  }
}

a.bar() this which refers to a which is what I want. I'm looking for a way for the child method inside a.lol.baz to also have this refer to a. Is there anyway to do it?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can't refer to it directly. But you can bind the function to the object explicitly, then this will be available.

a = {
  foo: 1,
  bar() {
    return this.foo + 1
  },
  lol: {}
}

a.lol.baz = (function() {
  return this.foo
}).bind(a);

console.log(a.lol.baz());

about 4 years ago · Juan Pablo Isaza Denunciar

0

you can use a.lol.baz.call(a) function

a = {
  foo: 1,
  bar() {
    return this.foo + 1
  },
  lol: {
    baz() {

      return this.foo
    }
  }
}
var res=a.lol.baz.call(a)
alert(res)

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