Javascript variable scope and un-named functions, what are the rules?
I've recently started developing javascript using jQuery and so far I'm really enjoying it.
Being a professional programmer (mainly C, C++, java and python) there is one thing that's been puzzling me regarding variable scope and unnamed function declarations. Let me exemplify:
------------------------
var sum = 0;
$(xmlobj).find("item").each(function(){
sum += parseFloat($(this).attr('value');
});
alert(sum); //this will alert the sum of each item
-----------------------
BUT
----------------------
function itemprocessing(){
sum += parseFloat($(this).attr('value));
}
var sum = 0;
$(xmlobj).find("item").each(itemprocessing);
alert(sum);
-----------------------------
will obviously fail since sum is not in the scope of the 'itemprodcessing' function.
So my question is, what are the variable scope rules for unnamed functions that make the first example above work?.