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

88
Views
¿Cómo definir una función de propiedad en la propiedad getter?

¿Hay alguna manera de agregar una propiedad en una función getter?

Idealmente, el código que quiero escribir es:

 const obj = { log: ['a', 'b', 'c'], get latest() { return this.log[this.log.length - 1]; } }; obj.latest.test = () => 123; console.log(obj.latest); // expected output: "c" console.log(obj.latest.test()) // expect output: 123

Mi deseo real es este: dado que mi getter realiza un cálculo bastante grande, por ejemplo:

 const obj = { otherproperties: ['a', 'b', 'c'], get items() { // VERY HUGE COMPUTATION... return [item0, item1, item2, item3] } };

y a veces solo necesito un elemento del resultado, quiero escribir algo:

 obj.items.withId = (id) => { // Don't run the entire huge computation but.. return computeOnlyId(id); }

para poder escribir obj.items si quiero todos los valores, u obk.items.withId(1) si quiero solo un elemento específico.

Probablemente, editando esta pregunta, ya entendí que esto es imposible ya que los items son captadores, pero les dejo los teclados para las respuestas.

Lo que me gustaría evitar es tener que escribir obj.items.all() para obtenerlos todos y obj.items.withId(id) para obtener solo una vez.

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

obj.latest será el resultado de this.log[this.log.length - 1] que será 'c' .

Dado que 'c' es una primitiva, no puede (útilmente) asignarle propiedades.

about 4 years ago · Santiago Gelvez 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!