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

161
Visualizações
Is `this` necessary to access member variables of typescript classes?

In the class below, I use this 9 times to deal with member variables and functions. It's clogging up all my beautiful code! Is there anything that I can do to make it prettier? For example, is there anyway to access the context member variable without referring to this?

//controls timing and game phases.

import { Context } from "./helpers/Context"
import { Model } from "./Model"
import { View } from "./View"

export class Controller {

    context : Context;

    constructor(context : Context) {
        this.context = context;
    }
    
    //make other objects in context, add them in.
    begin = () : void => {
        this.context.model = new Model(this.context);
        this.context.view = new View(this.context);
        this.init();
    } 

    init = () : void => {
        this.context.model.init();
        this.context.view.init();

        //causes an error! help.
        this.context.model.preloadModels([ 
            "/models/bleachers.obj"
        ], () => { this.buildWorld(); })
    }

    buildWorld = () : void => {
        this.context.view.makeGrass();
        this.context.view.makeSkybox();
        this.context.view.makeBleachersOnEdgeOfField();
    }

}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If you're used to languages like C or Java where you don't have to use this for class fields, it can look weird at first. I thought the same thing too. But for languages like Javascript and Python, it's normal to write this and self a lot of times. I think it's just a language-specific style thing that can look ugly to someone who is not used to seeing it, but this is the normal way and most JS programmers would not think it's ugly because they are used to it.

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