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

159
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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