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

107
Views
¿Cómo puedo agregar una propiedad calculada por una extensión de JavaScript con C#?

Tengo matriz y quiero acceso por propiedad (no función) como:

 let arr = [1,2,3]; console.log("last element: ", arr.last)

Intento que me guste, pero tengo un error:

 Array.prototype.last = (() => { return this[this.length - 1]; })();

Actualización 1: sí, sé cómo hacerlo como el método de extensión:

 Array.prototype.mysort = function() { this.sort(() => Math.random() - 0.5); return this; } myarray.mysort();

pero ¿cómo le gusta la propiedad?

 myarray.mysort;
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Puedes hacer esto con un captador . Debe usar Object.defineProperty() para agregar un captador a un objeto existente, en este caso, el objeto Array.prototype .

 Object.defineProperty(Array.prototype, "last", { get: function() { return this[this.length - 1]; } }); const a = [1, 2, 3]; console.log(a.last);

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!