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

101
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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