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

99
Views
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 answers
Answer question

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