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

115
Visualizações
Is this possible to design a function which takes a parameter and returns an object which holds multiple properties or functions

For example:

var resultList = [];
var objectName = (userName) => {

};

objectName.rowCount; // return overall count
objectName.fetchNext(); // updated data will be available in resultList

I have tried multiple solutions with no result like

var resultList = [];
var objectName = (userName) => {
  var rowCount = 0;
  init() {
    // make call to server and read data
    rowCount = 5; // setting dummy data
  };

  fetchNext = function(){
       // logic here
       resultList = [] // new data
  };

  
  init();
};

EDIT

Another attempt

var x = function(){
    var a = function(){console.log('a');};
    var b = function(){console.log('b');};
    return {a: a, b: b};
}
x.a(); // not able to call this function
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can't use an arrow function as a constructor, so tyou could change your code to use a traditional function:

function objectName(userName)  {
  var rowCount = 0;
  init = function() {
    // make call to server and read data
    rowCount = 5; // setting dummy data
  };

  this.fetchNext = function(){
       // logic here
       const resultList = [] // new data
       return resultList;
  };

  
  init();
};

var myObj = new objectName("foo");
console.log(myObj.fetchNext());

Or, you can return an object from your arrow function

var objectName = (userName) => {
  var rowCount = 0;
  function init() {
    // make call to server and read data
    rowCount = 5; // setting dummy data
  };

  
  
  init();
  
  return {
    fetchNext: function(){
       // logic here
       const resultList = [] // new data
       return resultList;
    }

  }
};

var myObj = objectName("Foo");
console.log(myObj.fetchNext());

Related:

  • Are 'Arrow Functions' and 'Functions' equivalent / interchangeable?

For completeness, the reason your edit did not work is that you defined x but never executed the function. This works:

var x = function(){
    var a = function(){console.log('a');};
    var b = function(){console.log('b');};
    return {a: a, b: b};
}
x().a(); // must execute x to get the result

And is esssentially the same as my second example above

about 4 years ago · Juan Pablo Isaza Relatório

0

In addition to the other answer, if you want to use an arrow function you can use it like this by wrapping the object in paranthesis:

const objectName = (userName) => ({
  rowCount: 0,
  resultList: [],
  init() {
    this.rowCount = 5;
  },
  fetchNext() {
    // fetch results
    this.resultList = [1, 2, 3, 4];
  }
});

const results = objectName('kyroath');
console.log('before init():', results.rowCount); // 0
results.init()
console.log('after init():', results.rowCount); // 5
console.log('before fetchNext():', results.resultList); // []
results.fetchNext()
console.log('after fetchNext():', results.resultList); // [1, 2, 3, 4]

about 4 years ago · Juan Pablo Isaza Relatório

0

Are you just looking for a Class?

class Obj {
    constructor() {
        this.rowCount = 0;
        this.resultList = [];
    }

    init() {
        // make call to server and read data
        this.rowCount = 5; // setting dummy data
    }

    fetchNext() {
        // logic here
        this.resultList.push(1); // new data
        return this.resultList;
    }
}

const obj = new Obj("userName");
obj.init();
const row = obj.rowCount;
const res = obj.fetchNext();

console.log(row);
console.log(res);

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