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

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

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 Denunciar

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 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