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

298
Visualizações
Get the generator function's name from a generator

In Javascript, given a generator object, how do I get it to output the name of the generator function that returned back that generator object?

In other words:

function* thisIsMyName(i) {
  yield i;
}

const gen = thisIsMyName(10);

console.log(gen.name); // How do I get this to output "thisIsMyName" using only the gen object?

Calling gen.name doesn't work as it isn't a function but a generator object

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

I am afraid you cannot access it like that because the reference to the function that generated this generator is stored in a property defined by a symbol. Symbols are unique and cannot be recreated (but nut all the time, see Symbol.for). Meaning that if you don't have the reference to a symbol you cannot get its value.

Object.getOwnPropertySymbols() does not return the native ones.

You can see however the symbol props in the console when inspecting the instance:

Console

You can solve this however with a wrapper function, meaning a function that will return both the generator and the generator function:

function thisIsMyName(value){
  const generatorFunction = function* thisIsMyName(i) {
    yield i;
  }
  return {
    generator : generatorFunction(value),
    generatorFunction: generatorFunction
  }
}
const gen = thisIsMyName(10);

console.log(gen.generatorFunction.name) // prints thisIsMyName
console.log(gen.generator.next()) // prints {value: 10, done: false}
about 4 years ago · Juan Pablo Isaza Relatório

0

From the comments:

You would need to create an intermediate factory that passed a reference to the function as well as the returned generator object

Not really usefull, but it may be something like:

const someGenerator = {
  *thisIsMyName(i = 1, limit = 10) {
    while (i < limit) {
      yield i++;
    }
  },
  get name() {return this.thisIsMyName.name},
};

const myGen = { name: someGenerator.name, i: someGenerator.thisIsMyName(15, 20)};
console.log(myGen.i.next().value, myGen.name);

Or ...

function createGenerator(name, min = 1, max = 10) {
  function* xGen() {
    while (min < max) {
      yield min++;
    }
  };
  
  return {gen: xGen(), name,};
};

const myGen = createGenerator(`myName`, 15, 20);
const values = [];
let nxt = {};
while (!nxt.done) {
  nxt = myGen.gen.next();
  !nxt.done && values.push(nxt.value);
}
console.log(values.join(`, `), `\nmyGen.name = '${myGen.name}'`);

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