On Jul 17, 5:23 pm, Peter van der Zee <[email protected]> wrote: > On Sun, Jul 17, 2011 at 2:26 AM, Matthew Bramer <[email protected]> wrote: > > So, I'm taking away from your answer that I should in general use .call > > I have to tell you, I have a hard time following what you have said though. > > I'm pretty new to this stuff, so some of your answer is clearly above my > > knowledge level. Sometimes, I wish I had an easy button for JS. :/ > > Cheers, > > Matt > > Ok, let me try ;) > > Let's say you have a function with three parameters, foo bar baz -> > > function boo(foo,bar,baz){ alert(foo+bar+baz); } > > Now `call` and `apply` allow you to set the "context" (`this` value)
ECMA-262 defines an execution context (the only use of context) as involving many things, including a scope chain, local variables and parameters and the this keyword. Limiting "context" to just this does not seem reasonable or appropriate. Just call it the this keyword or value. > of a function, that's what the first parameter is for. But let's skip > that for now (I use `null` for it). Noting that doing so will mean that the called function's this keyword will reference the global object (except in ES5 strict mode). > Then the only difference left between `call` and `apply` is that apply > will map the second parameter (which must be an array) No, it need not be an array. It can be an array-like object, such as an arguments object or a NodeList, or any object with a non-negative integer length property, see ECMA-262 §15.3.4.3. -- Rob -- 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]
