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

140
Views
Accessing Object within its property's getters

In this simplified code, I want to give someGetters a parameter and to return a specific text, but the getters can't seem to access the parent Object's "this", "this" is pointed to someGetters.

var constants = {
    FLAG2: "testx2",
    FLAG4: "testx4",
    FLAG6: "testx6",
    someGetters: {
        get [this.FLAG2]() { // tried "this" but it points to someGetters
            return "Test Test"
        },
        get [constants.FLAG4]() { // tried constants but it says it's not defined yet
            return "Test Test Test Test"
        },
        get ["testx6"]() { // but this works
            return "Test Test Test Test Test Test"
        }
    }
};

console.log(constants.someGetters[constants.FLAG2]) // expected result: Test Test

Note that constants is exported from another js file called constants.ts.

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

0

You can't refer to the object's properties until you've finished constructing it. So add someGetters after you've assigned constants, and then refer to constants rather than this.

var constants = {
  FLAG2: "testx2",
  FLAG4: "testx4",
  FLAG6: "testx6",
};

constants.someGetters = {
  get [constants.FLAG2]() {
    return "Test Test"
  },
  get [constants.FLAG4]() {
    return "Test Test Test Test"
  },
  get ["testx6"]() { // but this works
    return "Test Test Test Test Test Test"
  }
}

console.log(constants);

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!