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

225
Views
¿Cómo puedo obtener funciones getter en javascript?

Estoy tratando de obtener getter de obtención en la instancia de javascript.

Ideal (pseudo):

 class T { internal values = a, b getters = c, d } const getters = [] for (key in test) { if (test[key] is getter) getters.push(test[key]) } getters.forEach(getter => (getter.bind(test))()) // calls c() and d()

Pensé que Object.getOwnPropertyDescriptors me podía dar un getter s, pero no funcionó.

Vi que el prototipo (si hago una instancia a través class ) tiene getter s, pero no puedo acceder a estos valores.

Mi código:

 class Test { constructor() { this.a = 1; this.b = 2; } get c() { return this.a; } get d() { this.a + this.b; } } const test = new Test(); Object.getOwnPropertyDescriptors(test.__proto__); /* Object { constructor: {…}, c: {…}, d: {…} } c: Object { get: c(), enumerable: false, configurable: true, … } constructor: Object { writable: true, enumerable: false, configurable: true, … } d: Object { get: d(), enumerable: false, configurable: true, … } */ test.__proto__.c; // undefined test.__proto__.c(); // Uncaught TypeError: test.__proto__.c is not a function

Hay c en el prototipo, pero no puedo acceder a estos valores.

¿Hay alguna manera de obtener estos getter ?

Si existe un enlace que tengo que leer, notifíqueme. Gracias.

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

0

Debe extraer la propiedad get del descriptor y luego usarla; si intenta acceder a la propiedad .c directamente, estará invocando el getter (o setter).

 class Test { constructor() { this.a = 1; this.b = 2; } get c() { return this.a; } get d() { this.a + this.b; } } const test = new Test(); const testGetters = Object.values(Object.getOwnPropertyDescriptors(Object.getPrototypeOf(test))) .filter(descriptor => descriptor.get) .map(descriptor => descriptor.get); console.log(testGetters); console.log('Invoking getter manually:', testGetters[0].call(test));

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!