Here[s one more thought to add to the excellent advice you've gotten
already.
Reading between the lines a bit, one question seemed to be along these
lines: "Since .call() and .apply() are so similar, is there one of them that
I could use all the time instead of the other?"
The answer to this is yes: You really never have to use .call(); you can
always use .apply() instead.
.call() is merely a convenience method to let you write slightly cleaner
code when you know the number of arguments. It doesn't do anything that you
couldn't do with .apply(). Every use of .call() could be changed to an
.apply() by simply adding [] in the argument list. For example:
fun.call( obj, a, b, c );
can be coded instead as:
fun.apply( obj, [ a, b, c ] );
But you can't always go the other way around. Of course, *this* particular
use of .apply() could be converted back to a .call(), the same .call()
listed above. But you may not know the number of arguments you want, as in
Peter's max example and Rob's function wrapper example. In cases like those,
you can't use .call(), but .apply() will do the trick.
Of course, when reading other people's code you will run into both .call()
and .apply(), so it's good that you're asking questions and getting familiar
with both.
-Mike
--
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]