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

160
Vistas
shorthand way to reference `this` propeties?

I have a class

class Foo {
  constructor(x, y) {
    this.x = x
    this.y = y
  Bar() {
    console.log(this.x, this.y)
}

I recall seeing a method of referring to this. variables without having to specify this. Something like

class Foo {
  constructor(x,y) {
    this.x = x
    this.y = y
  Bar() {
    x,y = [this] // something like this
    console.log(x, y) // equivalent to this.x, this.y
}

I desire this because I have dozens of statements that call methods on this.ctx - like this.ctx.rect() - and I don't want to have to have this. prepended to all of them for the sake of readability.

Is there a syntax for this?

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

0

You can deconstruct this like any JavaScript object,

Bar() {
    const {x, y} = this;
}

I do not recommend doing this however, it seems like it would not scale well as a technique.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Objects (including arrays and functions) are handled by reference, meaning that if you write

let a = this.ctx;

then a points to the same object as ctx and you can do a.rect().

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