I wanted to add getter to Array.prototype to get the last element of the array.
I did it like this:
Object.defineProperty(Array.prototype, 'last', {
get: function() {
return this[this.length - 1];
}
});
Is this right for memory? My concern was if you instantiated 10,000 objects:
- I hope I only have 1 function in memory
- My concern is that I can have 10,000 * 1 = 10,000 functions in memory
My goal is to use it as follows:
const arr = [{}, {}, {}, {}];
arr.last === arr[arr.length - 1];
source
share