I've just learned the convention for popping off the first element of the arguments
array (which I also learned is actually an Object
). Now I need to do the opposite. I need to use an unshift
operation to add a value to the beginning of the arguments
array (or Object
acting like an array). Is this possible? I tried:
Array.prototype.unshift.apply('hello', arguments);
That had no effect on arguments
whatsoever.
I've just learned the convention for popping off the first element of the arguments
array (which I also learned is actually an Object
). Now I need to do the opposite. I need to use an unshift
operation to add a value to the beginning of the arguments
array (or Object
acting like an array). Is this possible? I tried:
Array.prototype.unshift.apply('hello', arguments);
That had no effect on arguments
whatsoever.
1 Answer
Reset to default 21use
.call()
instead of.apply()
to invokeunshift()
set
arguments
as thethis
value ofunshift()
set
'hello'
as the argument tounshift()
Array.prototype.unshift.call(arguments, 'hello');
As @lonesomeday pointed out, you can use .apply()
instead of .call()
, but you need to pass an array-like argument as the second argument. So in your case, you'd need to wrap 'hello'
in an Array.
arguments
collection. You can modify the argument values, but not the length of the collection etc. – Pointy Commented Nov 11, 2013 at 18:51undefined
, but updating the length of thearguments
object won't do that. I should have worded my comment more clearly. – Pointy Commented Nov 11, 2013 at 18:57