Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

108
Vistas
How can I add a property calculated by a JavaScript extension with C#?

I have array and want access by property (not function) like:

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

I try like it, but got error:

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

Update 1: yes i know how do it like extend method:

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

but how do it like property?

myarray.mysort;
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can do this with a getter. You need to use Object.defineProperty() to add a getter to an existing object, in this case the Array.prototype object.

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda