To add the size() method to the Array object I do:
if (!Array.size) {
Array.prototype.size = function() {
return this.length;
};
}
Is there a simple way to define the size property that will work like length ?
(I don’t really need it, I just want to understand if this is something easily achievable in Javascript.)
With ES5 it’s possible. Add a
sizeproperty on theArrayprototype,It basically works as a wrapper around length. Appears like a property to the naked eye, but is backed by an underlying function.
Thanks to @Matthew’s comment, this
sizeproperty works like a complete wrapper around length.