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

163
Views
Can not access private field through public get via proxy

When getting Data from a VueX Module via Getter the Object is wrapped with a Proxy that throws following error:

TypeError: attempted to get private field on non-instance

when trying to read a private property with a public getter.

class Example {
  #privateField;

  get privateField() {
    return this.#privateField;
  }
}
computed(){
  getInfoForSpecificItem() {
    const arrayOfInstantiatedExamples = this.$store.getters['getArrayOfInstantiatedExamples'];
    for (let i = 0; i < arrayOfInstantiatedExamples.length; i += 1) {
      // trying to access it in this condition throws the error since the Object
      // has been wrapped with a Proxy coming from Vue (I guess)
      if (arrayOfInstantiatedExamples[i].privateField === 'something') {
        // does something
      }
    }
  }
}

Why am I not able to read the property, and how can I create an instance from the proxy target. logging the Proxy Object reveals, that the target has all information, yet trying to read it via getter doesn't work.

Are there any other options than making the fields public?

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

0

You are defining a class not a function. So remove the parentheses().

class Example {

You cannot define variable with #. Only $ and _ are allowed to use in a variable. So replace # with $ like this $privateField;

Classes are blueprints, not a valid object. First make an object from this class.

const proxyWrappedExample = new Example;

After you log this then you can avoid the error but it logs undefined.

Your code should look like

class Example {
  $privateField = 'something';

  get privateField() {
    return this.$privateField;
  }
}

const proxyWrappedExample = new Example;

// I receive the Object (which has been instantiated earlier) wrapped in a proxy Object. 
// Trying to access the property throws the error.
// e.g.:

console.log(proxyWrappedExample.privateField); // 'something'

$privateField without any value will look like following.

class Example {
  $privateField;

  get privateField() {
    return this.$privateField;
  }
}

const proxyWrappedExample = new Example;

// I receive the Object (which has been instantiated earlier) wrapped in a proxy Object. 
// Trying to access the property throws the error.
// e.g.:

console.log(proxyWrappedExample.privateField); // 'undefined'
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!