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
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 Respuestas
Responde la pregunta

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