This post was published on Tuesday 22nd of January 2008.
arguments into an array...againA couple of months ago I wrote a little post about a trick to turn the built-in, array-like JavaScript variable arguments into an actual array that supports methods like slice, sort, etc. Today I found out that this little trick actually doesn't work in Apple's most popular browser: Safari. Here's a small addendum to that post.
The original trick was this:
var args = [].splice.call(arguments,0);
But unfortunately this doesn't work in Safari, the variable args just becomes an empty array. At first I thought the principle of calling an array method on arguments just didn't work but when a co-worker suggested trying the join() method that turned out to actually work. After a bit of experimenting I finally settled on the following snippet:
var args = [].slice.call(arguments,0);
Looks remarkably similar, doesn't it? The difference is just one letter: I call the slice method, in stead of the splice method. The slice method is very similar to splice in it's core functionality with one major difference: splice modifies the original array where slice doesn't. This, I believe is the problem for Safari when calling splice on arguments: this object is not modifiable. None of the other browsers seem to mind, though.
Have something to say about this post? Share your thoughts!
This post was published on Tuesday 22nd of January 2008. The previous entry was "JavaScript closures in for-loops". The next entry is "Out with the old...".
arguments into an array...again