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

103
Views
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 answers
Answer question

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 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!