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

117
Vistas
Can I programatically define a class with private fields and methods without eval?

The MDN docs for private fields mention:

It is a syntax error to refer to # names from out of scope. It is also a syntax error to refer to private fields that were not declared before they were called, or to attempt to remove declared fields with delete.

Outside of private fields, I can define a function which returns a constructor which can then be invoked as a class:

function CtorCreator(...) {
   const CtorBeingCreated = function Whatever(...) {...}
   // programmatically manipulate CtorBeingCreated
   return CtorBeingCreated
}

const Ctor = CtorCreator(...)
const instance = new Ctor();

In this I can programmatically define fields and methods based on arguments passed to the CtorCreator function. For example

...
   CtorBeingCreated.prototype[...] = ...
...

Can I also define private fields and methods that way? E.g. some way to allow me to do

   ...
   addPrivateFieldToCtor(CtorBeingCreated, <field-name>, <initial-value>)
   // or 
   addPrivateMethodToCtor(CtorBeingCreated, <method-name>, <method-body>) // method body can access <field-name> previously defined  
   ...
about 4 years ago · Juan Pablo Isaza
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