Why Does Array.Prototype.Push Return the New Length Instead of Something More Useful

Why does Array.prototype.push return the new length instead of something more useful?

I posted this in TC39's communication hub, and was able to learn a bit more about the history behind this:

push, pop, shift, unshift were originally added to JS1.2 (Netscape 4) in 1997.

There were modeled after the similarly named functions in Perl.

JS1.2 push followed the Perl 4 convention of returning the last item pushed.
In JS1.3 (Netscape 4.06 summer 1998) changed push to follow the Perl 5 conventions of returning the new length of the array.

see original jsarray.c source

/*
* If JS1.2, follow Perl4 by returning the last thing pushed. Otherwise,
* return the new array length.
*/

javascript push returning number instead of object

Array#push method works in situ, you don't have to assign it to a new variable. It won't return a new array, but will modify the original one and will return it's length. That's why you are getting 3 as the result.

To get the desired result, just call it, without assigning to any variable:

reminders.push(newReminder);

Array.push return pushed value?

Are there any substantial reasons why modifying Array.push() to return the object pushed rather than the length of the new array might be a bad idea?

Of course there is one: Other code will expect Array::push to behave as defined in the specification, i.e. to return the new length. And other developers will find your code incomprehensible if you did redefine builtin functions to behave unexpectedly.

At least choose a different name for the method.

You would then be able to do something like this: someFunction(value, someArray.push({}));

Uh, what? Yeah, my second point already strikes :-)

However, even if you didn't use push this does not get across what you want to do. The composition that you should express is "add an object which consist of a key and a value to an array". With a more functional style, let someFunction return this object, and you can write

var someArray = [],
value = "hello world";

function someFunction(value, obj) {
obj["someKey"] = value;
return obj;
}

someArray.push(someFunction(value, {}));

How can I extend Array.prototype.push()?

Since push allows more than one element to be pushed, I use the arguments variable below to let the real push method have all arguments.

This solution only affects the arr variable:

arr.push = function () {
//Do what you want here...
return Array.prototype.push.apply(this, arguments);
}

This solution affects all arrays. I do not recommend that you do that.

Array.prototype.push = (function() {
var original = Array.prototype.push;
return function() {
//Do what you want here.
return original.apply(this, arguments);
};
})();

React - Array are passed as their length

This is the problem:

setRegionWithValue(regionWithValue.push(el));

push returns the length and not the array you pushed to

You can do it like this:

setRegionWithValue(v => ([...v, el]));

See: Why does Array.prototype.push return the new length instead of something more useful?

Stating from Array.prototype.push() MDN docs:

The push() method adds one or more elements to the end of an array and returns the new length of the array.



Related Topics



Leave a reply



Submit