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

121
Views
How to add private data to an arbitary object without using a WeakMap

Ok. This isn't really a question but rather an epiphany I had today that I would like to share, because I don't know if anyone realizes it.

Consider the following code:

function Id(obj) {
    return obj;
}

class Meta extends Id {
    #data;

    constructor(obj, data) {
        super(obj);
        this.#data = data;
    }

    getData() {
        try {
            return this.#data;
        } catch (ex) {}
    }

    setData(value) {
        try {
            this.#data = value;
        } catch (ex) {
            new Meta(this, value);
        }
    }

    static get = Function.call.bind(this.prototype.getData);
    static set = Function.call.bind(this.prototype.setData);
}

var obj = Object.freeze({}); // !!!! frozen
console.log(Meta.get(obj)); // undefined
Meta.set(obj, "test");
console.log(Meta.get(obj)); // "test"
console.log(Reflect.ownKeys(obj)); // Array []

As you can see, I added a private variable to an external frozen object that can be accessed only by my code.

Thank you for reading and enjoy!!!!

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!