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

100
Vistas
Access proxied properties internally

I'm using model classes that extend a base Model class. The Model class returns a Proxy from the constructor that allows me to access properties passed into the constructor without having to explicitly define them on the class.

Using this, I can access a property of the data object externally, but attempting to access it internally returns undefined. How can I access the proxied properties from within the class itself?

class Model {
  constructor(data) {
    this.data = data;

    return new Proxy(this, {
      get(model, prop) {
        if (model.hasOwnProperty(prop) || prop in model) {
          return model[prop];
        }

        if (model.data.hasOwnProperty(prop)) {
          return model.data[prop];
        }
      }
    });
  }
}

class User extends Model {
  get name() {
    return `${this.first_name} ${this.last_name}`;
  }
}


const user = new User({
  first_name: 'Bob',
  last_name: 'Smith'
});

console.log(user.first_name); // 'Bob'
console.log(user.name); // undefined undefined

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You need to use Reflect.get() to access properties on the original object

Reflect

Reflect is a built-in object that provides methods for interceptable JavaScript operations. The methods are the same as those of proxy handlers. Reflect is not a function object, so it's not constructible.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Reflect

Reflect.get()

The static Reflect.get() method works like getting a property from an object (target[propertyKey]) as a function.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Reflect/get

class Model {
  constructor(data) {
    this.data = data

    return new Proxy(this, {
      get(model, prop) {
        if (model.data.hasOwnProperty(prop)) {
          return model.data[prop]
        }
        return Reflect.get(...arguments)
      },
    })
  }
}

class User extends Model {
  get name() {
    return `${this.first_name} ${this.last_name}`
  }
}

const user = new User({
  first_name: 'Bob',
  last_name: 'Smith',
})

console.log(user.first_name) // 'Bob'
console.log(user.name) // 'Bob Smith'

Reflect.get takes 3 arguments: the object, the key, and and optional this context.

Here Reflect.get(...arguments) is equivalent to Reflect.get(model, prop)

How to use Reflect.get

Reflect.get({ x: 1, y: 2 }, 'x')  // 1
Reflect.get(['zero', 'one'], 1)  // "one"
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