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

143
Views
Accediendo a Object dentro de los captadores de su propiedad

En este código simplificado, quiero dar a someGetters un parámetro y devolver un texto específico, pero parece que los getters no pueden acceder a "this" del Objeto principal, "this" apunta a 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

Tenga en cuenta que constants se exportan desde otro archivo js llamado constants.ts .

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

0

No puede hacer referencia a las propiedades del objeto hasta que haya terminado de construirlo. Por lo tanto, agregue someGetters después de haber asignado constants y luego consulte constants en lugar de 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!