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

191
Vistas
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 Respuestas
Responde la pregunta

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 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