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

201
Visualizações
Override method of the class that inside the function

I have the below structure in file a.js

function factory() {
    class A {
        constructor() {
            
        }

        add(num) {
            return num + 1;
        }
    }

    return A;
}

I would like to change the behavior of the "add" method. "add" method would look like below. Addition of 2 instead of 1.

add(num) {
    return num + 2;
}

I have created a new file name as b.js

const a = import("a.js");
(new factory()).prototype.add = function(num) {
    return num + 2;
}

But it does not change the behavior of the add method inside the "factory" function(a.js file). I would like to change the behavior of the "add" method of the "factory" function(a.js), Could you please provide help or guidance?

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

0

Each call to factory function will create and return a new class.

So adding the add method on the prototype of a class returned by the particular call to factory function will have no effect on the classes returned by other calls to the factory function.

Following demo shows how you can achieve the desired result.

function factory() {
  class A {
    add(num) {
      return num + 1;
    }
  }

  return A;
}

const MyClass = factory();

MyClass.prototype.add = function (num) {
  return num + 2;
};

const b = new MyClass();
console.log(b.add(1));

If you call the factory function again, it will return a new class which will have the add method that adds only 1.

Following demo shows this in action:

function factory() {
  class A {
    add(num) {
      return num + 1;
    }
  }

  return A;
}

const MyClassA = factory();
const MyClassB = factory();

// Overwrite "add" method in MyClassA
MyClassA.prototype.add = function (num) {
  return num + 2;
};

console.log(new MyClassA().add(1)); // 3
console.log(new MyClassB().add(1)); // 2 (MyClassB uses the defaul "add" method)

So, you will need to add the add method in the prototype every time factory function is called.


One way to avoid overwriting the add method everytime you call factory function is to create another function, that:

  1. Calls the factory function
  2. Updates the add method
  3. Returns the class with the updated add method

With this approach, code that overwrites the add method is re-usable.

Following code shows an example:

function factory() {
  class A {
    add(num) {
      return num + 1;
    }
  }

  return A;
}

function factoryWrapper() {
  const MyClass = factory();

  MyClass.prototype.add = function(num) {
    return num + 2;
  };
  
  return MyClass;
}

const Class1 = factoryWrapper();
const Class2 = factoryWrapper();

console.log(new Class1().add(1));
console.log(new Class2().add(1));

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