I wanted to add a 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 proper for memory? My worry was if you instance 10000 objects:
- I hope I will only have 1 functions in the memory
- My worry is that I might have 10000 * 1 = 10000 functions in the memory
My goal is to use it like this:
const arr = [{}, {}, {}, {}];
arr.last === arr[arr.length - 1];