On 18 July 2011 07:44, Michael Geary <[email protected]> wrote: > > 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
When in doubt, use both! Ok, not unless you really have to, but a fun fact is that since call and apply are Functions, they can themselves be called and applied. You'll run into this if you ever want to call certain native functions with call() and apply() in some versions of IE. The following are all equivalent: // Call apply() in the context of document.getElementById with the arguments: document, ['header'] Function.prototype.apply.call(document.getElementById, document, ['header']) // Apply apply() in the context of document.getElementById with the arguments: document, ['header'] Function.prototype.apply.apply(document.getElementById, [document, ['header'])) // Call call() in the context of document.getElementById with the arguments: document, header Function.prototype.call.call(document.getElementById, document, 'header') // Apply call() in the context of document.getElementById with the arguments: document, header Function.prototype.call.apply(document.getElementById, [document, 'header']) - Jonny -- 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]
