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

317
Vistas
Rewrite code into classes using object-oriented programming (OOP)

I made a rating as in the author's video: https://www.youtube.com/watch?v=dsRJTxieD4U

const rateStars = document.querySelectorAll('.js-rate');
rateStars.forEach((rateStar, clickedIdx) => {
  rateStar.addEventListener('click', () => {
    rateStars.forEach((otherRateStar, otherIdx) => {
      if (otherIdx <= clickedIdx) {
        otherRateStar.classList.add('rate__item_active');
      };
    });
  });
});

Everything works perfectly, but my task is to create a js-class (OOP)

enter image description here

  //rate.pug

mixin rate(params = {})
  -
    const {
      rating= "",
    } = params;
    let i = 0;


  .rate
    ul.rate__list
      while i < 5
        if (i < rating)
          li.rate__item(class= "js-rate rate__item_active")
        else
          li.rate__item(class= "js-rate")
        - i++ 

I got the following code structure:

// index.js
import Rate from './Rate';
const rateStars = document.querySelectorAll('js-rate');
rateStars.forEach((rateStar, clickedIdx) => new Rate(rateStar, clickedIdx));
        
// Rate.js
class Rate {
  constructor(rateStar, clickedIdx) {
    this.rateStar = rateStar;
    this.clickedIdx = clickedIdx;
    this.bindEventListeners();
  }

  bindEventListeners() {
    this.rateStar.addEventListener('click', this.handleRateClick.bind(this));
  }
      
  handleRateClick() {
    this.rateStar.classList.add('rate__item_active');
  }
}

export default Rate;

However, I have no idea how to proceed from here. Sorry I'm a complete noob. This is my first time asking a question here. the task is complicated by the fact that you need to place several ratings on one page.I write some mixins: enter image description here

When there are several ratings on the page, then the stars are added to all the ratings.

1)this is the default state

enter image description here

  1. After click

enter image description here

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

0

So the problem of your change to class, is that you're not adding rate__item_active to the previous stars as you're doing in the first code example.

So for your class to work like the first code example, this is how you should do it:

// index.js
import Rate from './Rate';
rateStars.forEach((rateStar, index) => new Rate(rateStar, index, rateStars));
    
// Rate.js
class Rate {
  constructor(rateStar, index, rateStars) {
    this.rateStar = rateStar;
    this.clickedIdx = index;
    this.rateStars = rateStars;
    this.bindEventListeners();
  }

  bindEventListeners() {
    this.rateStar.addEventListener('click', this.handleRateClick.bind(this));
  }
  
  handleRateClick() {
    for(let i = 0; i <= this.clickedIdx; i++) {
      this.rateStars[i].classList.add('rate__item_active');
    }
  }
}

export default Rate;
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