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

179
Views
Overriding object's default getter operator

Let's say I have the following object in my JS code:

var myData = {
  "someWeirdPrefix_name": "John Doe",
  "someWeirdPrefix_age": 24,
  ...
}

Is there a way to override the default getter operator (.)? As in, if I write myData.name, I would like to get "John Doe" (thanks to the override). Basically I want to override the method so that I can take the key given, add the weird prefix to the key and then get the value. I know the easy approach would be to just clean my actual data and remove the prefix, but that's not acceptable in my use case. Is this actually possible to do for every property (not just name and age)?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Usually Proxy is used for that

const myData = {
  "someWeirdPrefix_name": "John Doe",
  "someWeirdPrefix_age": 24,
}

const newMyData = new Proxy(myData, {
  get(target, prop, receiver) {
    return target['someWeirdPrefix_' + prop];
  }
})

console.log(newMyData.name)

about 4 years ago · Juan Pablo Isaza Report

0

I suggest you create a proxy over the data object, and on the get handler, you can check if there is a key in myData including the dot notation key, if YES, get the value of that key.

const myData = {
  "someWeirdPrefix_name": "John Doe",
  "someWeirdPrefix_age": 24,
}

const handler = {
   get(target, prop, receiver) {
    return target[Object.keys(target).find(key => key.includes(prop))]
  }
}

const proxy = new Proxy(myData, handler);

console.log(proxy.name)

about 4 years ago · Juan Pablo Isaza Report
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!