Hi Joe,

Array is a constructor which is a function, and Function has no slice
function.
Slice is actually defined on Array.prototype so you could say:

Array.prototype.slice.call(array,0)

This would be the most transparent syntax.

However since every array instance inherits from Array.prototype, all
of the following work too:

new Array().prototype.slice.call(array,0);
Array().prototype.slice.call(array,0);
[].prototype.slice.call(array,0);

All 4 approaches do almost the same thing (the only diff with the
latter 3 is they create a needless array instance and force a quick
search up the prototype chain - but it's a minor hit unless you do it
often) - it's mainly just a matter of style.

Hope this helps
Angus



On Jan 1, 8:54 am, Poetro <[email protected]> wrote:
> 2010/12/31 Joe <[email protected]>:
>
> > I'm running through John Resig's "Learning Advanced JavaScript" and I
> > ran into some questions when I got to slide #45 (http://ejohn.org/apps/
> > learn/#45).
> > In the example, an arguments collection is passed to a function which
> > is supposed to return a true array of the contents of arguments. I
> > figured the function would look something like this:
>
> > function makeArray(array){
> >  return Array.slice.call(array, 0);
> > }
>
> Since the Array function doesn't have a slice method, you could use
>
> function makeArray(array){
>  return Array.prototype.slice.call(array, 0);
>
> }
>
> > But this failed to work, and Resig's function, which worked, looked
> > like this:
>
> > function makeArray(array){
> >  return Array().slice.call( array );
> > }
>
> The Array() creates a new empty Array, that also has a slice method,
> but does another call, so it is not that elegant nor as fast as using
> the prototype's function.
>
> --
> Poetro

-- 
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/[email protected]/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/[email protected]/

To unsubscribe from this group, send email to
[email protected]

Reply via email to