I have some simple code you can see in my fiddle. It alerts properly in all browsers and IE9, but not IE8 or 7.
var func = function( x ) {
var slice = [].slice,
args = slice.call( arguments ),
pass = args.splice(1);
alert( pass );
};
func( 'a', 1, 2 );
EDIT Using the solution I posted what I used here: http://jsfiddle.net/7kXxX/4/
I am using this in a case where I don't know how many arguments are coming, which is why I'm using "arguments"
The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place.
Slice is used to get a new array from the original array whereas the splice is used to add/remove items in the original array. The changes are not reflected in the original array in the case of slice and in the splice, the changes are reflected in the original array.
Alternative of Array splice() method in JavaScript If the count is not passed then treat it as 1. Run a while loop till the count is greater than 0 and start removing the desired elements and push it in a new array. Return this new array after the loop ends.
The . splice() method is a destructive array method, which means it modifies the array on which it is called (disclaimer: destructive methods can be risky, especially if you use the array in question elsewhere in your program, so proceed with caution).
The ECMAScript 3rd edition standard requires the second deleteCount
argument:
Array.prototype.splice(start, deleteCount [, item1 [, item2[,...]]])
MSDN docs show that IE follows this standard:
arrayObj.splice(start, deleteCount, [item1[, item2[, . . . [,itemN]]]])
Firefox's SpiderMonkey allows the second argument to be optional (as do other modern browsers):
array.splice(index , howMany[, element1[, ...[, elementN]]])
array.splice(index[, howMany[, element1[, ...[, elementN]]]])
Description:
howMany An integer indicating the number of old array elements to remove. If howMany is 0, no elements are removed. In this case, you should specify at least one new element. If no howMany parameter is specified (second syntax above, which is a SpiderMonkey extension), all elements after index are removed.
Sources:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With