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

94
Vistas
call a methods of a class without using NEW keyword inside other class node js

I want to access Main class methods to another Person class without creating a new instance Is it possible?? Can we access it without creating an instance of a class

let myInstance = new Person();

 class Main {
      constructor(args) {
        this.hooks = [];
      }
      add_hooks(name, func) {
        if (!this.hooks[name]) this.hooks[name] = [];
        this.hooks[name].push(func);
      }
      call_hooks(name, ...params) {
        if (this.hooks[name]) this.hooks[name].forEach((func) => func(...params));
      }
    }

other class Person how to access without using new keyword

const Main = require("./main.js");
class Person {
  exec() {
    const action =  Main();
    action.add_hook("jump", console.log.bind(console, "this will log "));
  }
}
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

If you're not planning on instantiating the object, and you don't care about having multiple instances with each having their own state, you don't need a class.

Just create individual functions, or export an object.

const hooks = [];

export function add_hooks(name, func) {
  if (!hooks[name]) hooks[name] = [];
  hooks[name].push(func);
}

export function call_hooks(name, ...params) {
  if (!hooks[name]) return;
  for (const func of this.hooks[name]) {
    func(...params);
  }
}

It's possible too to do this with static methods, and that would be the likely answer if you write Java where everything has to be a class, but I wouldn't recommended it in Javascript.

about 4 years ago · Juan Pablo Isaza Denunciar

0

There is no big magic to it. Since the OP just wants to reuse prototypal Main methods, one is going to explicitly delegate the method/s of interest which was/were provided/accessed before via Main.prototype ...

class Main {
  constructor(args) {
    this.hooks = {};
  }
  add_hooks(name, func) {
    if (!this.hooks[name]) {
      this.hooks[name] = [];
    }
    this.hooks[name].push(func);
  }
  call_hooks(name, ...params) {
    if (this.hooks[name]) {
      this.hooks[name].forEach(func => func(...params));
    }
  }
}

// const Main = require("./main.js");

class Person {

  // // ... either add `hooks` as public property at instantiation time ...
  // hooks = {};

  exec() {
    const ref = Main.prototype;
    ref.add_hooks.call(this, "jump", console.log.bind(console, "this will log"));
  }
}

// ... or add `hooks` via additional glue code ...
function createPersonWithHooksAndExecute() {
  const type = new Person();

  type.hooks = {};
  type.exec();

  return type;
}
const someone = createPersonWithHooksAndExecute();
console.log({ someone });

// this will log
Main.prototype.call_hooks.call(someone, "jump");
.as-console-wrapper { min-height: 100%!important; top: 0; }

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