Good point, although I'd think the difference in performance can't be much. I could also imagine a JS implementation where .call() was slower - if it actually was implemented as a wrapper around .apply(). So in code where it mattered, I'd want to run some timing tests on the browsers or JS environments I needed to support.
Even more importantly for most code, .call() is simpler and cleaner too. I wasn't seriously suggesting that anyone should use .apply() when .call() would do. It's just helpful to know that .apply() is the more general function and .call() is a nice shortcut for the cases where it works. -Mike On Sun, Jul 17, 2011 at 11:17 PM, Xavier MONTILLET <[email protected]>wrote: > But call is faster than aplly. > On Jul 18, 2011 5:42 AM, "Michael Geary" <[email protected]> wrote: > > 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] > > -- > 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] > -- 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]
