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

87
Visualizações
Const Functions to an array from other file

I have 2 functions to const in my index.js:

const funcE = require("../Functions/cmdError")
const funcH = require("../Functions/cmdHelp")

When I call functions must be like this:

funcE.cmdError(args)
funcH.cmdHelp(args)

I want join 2 const lines into 1 line and how to call these funtions?

about 4 years ago · Santiago Gelvez
3 Respostas
Responde à pergunta

0

You have 3 possibilities to declare your constants in one line.

Option 1

The simplest way:

const funcE = require("../Functions/cmdError"); const funcH = require("../Functions/cmdHelp");

Option 2

Use a comma and don't use const for the second declaration.

const funcE = require("../Functions/cmdError"), funcH = require("../Functions/cmdHelp");

Option 3

Use Destructuring assignment.

The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.

const [funcE, funcH] = [require("../Functions/cmdError"), require("../Functions/cmdHelp")];

This is how you can invoke your functions in all cases:

funcE.cmdError(args)
funcH.cmdHelp(args)

This is what you can do, if you want to have 1 constant only:

const func = [require("../Functions/cmdError"), require("../Functions/cmdHelp")];

Invoke the functions with: func[0].cmdError(args) and func[1].cmdHelp(args)

OR

you could also declare an object.

const functions = {
  funcE: () => require("../Functions/cmdError"),
  funcH: () => require("../Functions/cmdHelp")
}

Invoke the functions with: functions.funcE() and functions.funcH()

about 4 years ago · Santiago Gelvez Relatório

0

You can also reach your goal by doing this:

Create a new file, for example, index.js. In this index.js file write your "requires" and export them.

const funcE = require("../Functions/cmdError");
const funcH = require("../Functions/cmdHelp");

module.exports = {
  funcE,
  funcH
}

Then you can require the index.js file where you want and access these functions.

about 4 years ago · Santiago Gelvez Relatório

0

I figured out how to do it already, thanks everyone.

const func = [require("../Functions/cmdHelp"), require("../Functions/cmdError")]

and call it:

func[0].cmdHelp(args)
func[1].cmdError(args)
about 4 years ago · Santiago Gelvez 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