I’m reading the documentation for the Underscore.js library from DocumentCloud. Many of the functions take an optional context argument which is not explained. My guess, as one familiar with Ruby is that this is similar to a Ruby binding. And that it has something to do with what this means. The extent of my JavaScript usage has been a few jQuery calls and some very boilerplate ajax.
My question: What does context mean and how should I use it? A good answer should probably contain some information about how JavaScript works as well.
Javascript functions take a hidden
thisparameter which indicates the context in which the function was called.Ordinarily,
thisis the global object (usuallywindow). However, when a function is called on an object,thiswill be the object that it was called on.Underscore.js methods that take callback functions take an optional
contextparameter. If this parameter is specified, the callback will be called with thatcontext, meaning thatthisinside the callback will be equal to the context.