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

233
Visualizações
Using classes as structures trying to set pure functions as methods

Usually, classes in JavaScript are presented like this:

class Rectangle {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
  get area() {
    return this.calcArea();
  }
  calcArea() {
    return this.height * this.width;
  }
}

As a fan of functional programming and as someone who want to make code scalable, I am tempted to have like this for the whole app instead:

calcArea({height, width}) {
    return height * width;
  }

class Rectangle {
  constructor(height, width) {
    this.height = height;
    this.width = width;
  }
  get area() {
    return calcArea(this);
  }
}

Basically transform classes into structures and make all methods with related functions abstracted. This way I can easily export the calcArea function Does this make sense, I would like to hear some thoughts on this, are there good articles about these architectures approaches? Thank you

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

0

FP facilitates code sharing because it clearly separates the state from its transition logic. I'd suggest to think about whether you really need to add all this supporting code, or you could just work with state and transitions?

Wouldn't the following be just as fine? And possibly clearer?

const rect = { width: 100, height: 500 };

const toRectArea = (rect) => rect.width * rect.height;
const toTriagleArea = (triangle) => toRectArea(trianle) / 2;

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