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);
}
But this failed to work, and Resig's function, which worked, looked
like this:
function makeArray(array){
return Array().slice.call( array );
}
Now, I thought call() took as its parameters a context object followed
by a comma delimited list of arguments for the called function. In the
case of slice() I assumed the context would be the arguments
collection, and the second argument (first argument of slice, which it
requires) would be the starting index value.
The way his function looks, it seems like he's included only the
context object and left out the required starting index of slice(),
but his function works and mine does not. Can anyone shed some light
on this?
As a bonus question, why did he use parentheses, saying
"Array().slice..." instead of just "Array.slice"?
- Joe
--
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]