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

110
Visualizações
Accesing exported module

Having such a simple module (exports.js):

module.exports.val1 = "boz";
exports.val2 = "bar";

module.exports.fun1 = function Something() {
  console.log("bla bla");
};

exports.fun2 = function Something() {
  console.log("bla bla");
};

exports = "abc"; //1
module = "def"   //2

console.log(module);
console.log(exports);

and the file importing it(index.js):

var imp = require("./exports")
console.log(imp)

i get on the output:

def 
abc 
{val1: "boz", val2: "bar", fun1: ƒ Something(), fun2: ƒ Something()}

How can the imports be accessible/defined in the index file since it's overwritten on the #1 & #2 in the module file itself (what can be observed getting this 2 strings from module logs)?

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

I believe it is the same as if you called a function with an object. The function gets a copy of the reference to the object and so it can manipulate the object, all reassignment does is make the local copy of the reference in the function point elsewhere.

function caller() {
   var obj = {}
   callMe(obj);
   obj.name; // "Hello"

}

function callMe(obj) {
   obj.name = "Hello";
   obj = {};
}

Note above that the obj the caller passes in is unaffected by the reassignment in "callMe". See "pass by value".

over 4 years ago · Santiago Trujillo 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