Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

287
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda