javascript - Array.push return pushed value? -
Is there any reason enough that Array.push ()
the length of the new array is a bad idea Might be possible?
I do not know whether it is already proposed or asked before; There are only innumerable questions related to the current functionality of Array.push ()
in Google searches
An example implementation of this functionality is, feel free to fix it: < / P>
; (Function () {var _push = Array.prototype.push; Array.prototype push = function} {return it [_push.apply (this, argument) - 1];}} ());
Then you will be able to do something like this:
var someArray = [], value = "Hello world"; Function Some functions (value, obez) {obj ["someKey"] = value; } Some functions (value, someArray.push ({});
Where some functions modify the object passed as the second parameter, for example. Now the content of
someArray
[["Somki": "Hello World"}]
.
Are there any drawbacks to this approach? There are no solid reasons to modify Array.push ()
: p>
Returning the push object instead of the new array can be a bad idea?
Of course there is one: to behave as other code defined in the Array :: push
specification, ie to return the new length. And other developers will get your code out of understanding, then you will have to redefine the built-in functions to behave unexpectedly.
Choose another name for at least the method.
After this you will be able to do something like this:
some functions (values, some arrows. Push ({});
Uh, what? Yes, my second issue is already attacked: -)
However, if you have not used push
, even then do not get it in what you want to do is. The composition you should express is "Add an object that contains a key and value for an array". With a more functional style, some functions
get this object back, and you can type
var someArray = [], value = "Hello World"; Function Some functions (value, obez) {obj ["someKey"] = value; Return obj; } SomeArray.push (some function (value, {}));
Comments
Post a Comment