era86
November 2nd, 2011, 12:16 AM
Calling all web devs!
The terms in the title are probably wrong, but I was wondering how Javascript handles the following while using JQuery:
$(".element").each(function() {
alert("HI");
});
vs
var hi = function() {
alert("HI");
}
$(".element").each(hi);
Let's say I have five of these classes ".element" in my document. Does it create five anonymous functions in the first case? Or is this optimized in some way? I imagine the second piece of code is how you want things to be done. I'm just curious and couldn't find a straightforward answer via Google. Thanks!
The terms in the title are probably wrong, but I was wondering how Javascript handles the following while using JQuery:
$(".element").each(function() {
alert("HI");
});
vs
var hi = function() {
alert("HI");
}
$(".element").each(hi);
Let's say I have five of these classes ".element" in my document. Does it create five anonymous functions in the first case? Or is this optimized in some way? I imagine the second piece of code is how you want things to be done. I'm just curious and couldn't find a straightforward answer via Google. Thanks!