On 15 September 2011 20:19, Matthew Bramer <[email protected]> wrote:
> I have another small question as a result of the code here. With this line
> of code:
> jsllc.GetHashTable( listName, options, function (hashTable) {
>
> the variable hashTable hasn't been defined within the function call. It's
> defined within GetHashTable as: hashTable. How does that work? I thought
> all vars had to be declared prior to using them... Also, in the line above,
> if I call the variable something else like: bugaboo
> Would that affect this function from working? This is where I get lost with
> callbacks and how mysterious parameters appear out of nowhere... Like using
> jQuery's .each(). With that function call, you can do:
> .each(function(index, element) { }
> or
> .each(function() { }
> and both are totally valid. Bizzarro...
What you're doing there is declaring an anonymous function. When you do:
function add(x, y) {
return x + y;
}
add(4, 5);
Then in the add function, x and y will be bound to 4 and 5 (for that
invocation). The same thing happens with jQuery.each(). The inside of
the each function will look a bit like this:
$.each = function (array, callback) {
for (var i = 0, len = array.length; i < len; i++) {
callback(i, array[i]);
}
};
So, index and item will be bound to each index and each item of the
array. Make sense? The same thing happens with your callback function
- the argument will be bound when the function is invoked by whoever
you passed it to.
Hope that makes sense :)
Nick
--
Nick Morgan
http://skilldrick.co.uk
@skilldrick
Save our in-boxes! http://emailcharter.org
--
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]