Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

108
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!