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

281
Vistas
ES6 class - accessing instance variables

I am working on a webapp where I have pages with multiple horizontally scrolling panels. I want to handle the horiziontal scroll via my own vanilla JS code. Given that I need to do this many times over I thought I would wrap that functionality in an ES6 class. My effort are along the following lines

class HoriScroll 
{
 constructor(id)
 {
  this.target = document.getElementById(id);
  this.target.addEventListener('touchstart',this.slideStart,{passive:true});
  this.target.addEventListener('touchmove',this.slideMove,{passive:true});
  this.target.dataset.startX = null;
  this.target.dataset.startTime = null;
  this.target.dataset.movePending = false;
 }

 slideStart(e)
 {
  if (1 === e.changedTouches.length) 
  {
   this.dataset.startX = e.changedTouches[0].screenX;
   this.dataset.startTime = Date.now();
  } 
 }
}

While I will be able to get things working this way what bothers me is the rather clumsy way I am recording startX, startTime etc - as attributes of the dataset of the horizontal scroll target HTML element. I ended up doing this since I cannot work out a way to access instance variable values in the class. For instance in slideStart this refers to the HTML element instance, not the HoriScroll class instance.

How, if at all, can this be done.

about 4 years ago · Juan Pablo Isaza
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