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

216
Visualizações
Vanilla js class factory

I was trying to implement some kind of factory that create a class definition in the run time,I figure this work my code look like this


 function JsClassFactory() {
    this.X = class {

    };
    this.addMethod = (name, def) => {
        let Y = class extends this.X {
            constructor() {
                super()
                this[name] = def
            }
        }
        this.X = Y
        return this;
    }

    this.addAttribute = (name, def) => {
        return this.addMethod(name, def)
    }

    this.getClass = function () {
        return this.X
    };
}


(function () {
    const cf = new JsClassFactory()
    cf.addAttribute('age', 35)
    cf.addAttribute('name', 'chehimi')

    cf.addMethod('myName', function () {
        console.log(' i am %s', this.name)
    })
    cf.addMethod('myAge', function () {
        console.log('my age is', this.age)
    })
    let z = new (cf.getClass())
    z.myName();
    z.myAge()
    cf.addAttribute('name', 'ali')
    cf.addAttribute('age', 15)
    let t = new (cf.getClass())
    t.myName();
    t.myAge()
})()

i am asking if there is a better a way to implement this feature? or a better work arround,

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

0

Here is the approach I would take to accomplish this.

class JSClassBuilder {
  constructor(){
    this.constructedClass = class {};
    this.constructedClassAttributes = {};
  }
  
  addStatic(name, definition){
    this.constructedClass[name] = definition;
    return this;
  }
  
  addAttribute(name, value){
    if(typeof definition != 'function'){
      this.constructedClassAttributes[name] = value;
      return this;
    }
    throw new TypeError("parameter value must not be of type: 'function'");
  }
  
  addMethod(name, definition){
    if(typeof definition == 'function'){
      this.constructedClass.prototype[name] = definition;
      return this;
    }
    throw new TypeError("parameter definition must be of type: 'function'");
  }
  
  constructClass(){
    return Object.assign(
      new this.constructedClass(), 
      this.constructedClassAttributes
    ); 
  }
}

const classBuilder = new JSClassBuilder();

const c = classBuilder
            .addAttribute('age', 35)
            .addAttribute('name', 'Chehimi')
            .addMethod('myName', function () {
                console.log('I am %s', this.name);
            })
            .addMethod('myAge', function () {
                console.log('I am %s', this.age);
            })
            .constructClass();
c.myName();
c.myAge();

const a = classBuilder
            .addAttribute('name', 'Ali')
            .addAttribute('age', 15)
            .constructClass();
a.myName();
a.myAge();

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