Are there any significant reasons why changing Array.push()to return an object, rather than the length of a new array, might be a bad idea?
I do not know if this has already been proposed or asked; Google searches returned only a huge number of questions related to current features Array.push().
Here is an example implementation of this feature, feel free to fix it:
;(function() {
var _push = Array.prototype.push;
Array.prototype.push = function() {
return this[_push.apply(this, arguments) - 1];
}
}());
Then you can do something like this:
var someArray = [],
value = "hello world";
function someFunction(value, obj) {
obj["someKey"] = value;
}
someFunction(value, someArray.push({}));
Where someFunctionchanges the object passed as the second parameter, for example. Now the contents are someArrayequal [{"someKey": "hello world"}].
Are there any flaws in this approach?
source
share