While I agree that var is often missing and global vars are often not
a good idea...

In JS vars are not scoped in a for or if.... so var inside the for (or
if) does little more than a var before it. This is the reason that we
have to resort to the confusing looking

function(){var x = 1...}();

when the code says:

        var i = 22;
        for (var i = 0; i < 5; ++i){};
        alert(i);
JS shows 5, any other normal language would show 22!

Javascript doesn't have block scoping, providing only global and
function scopes!

On 10/4/06, Dave Methvin <[EMAIL PROTECTED]> wrote:
>
> > Is there a way to set a variable in an each that will be stored
> > separately for each instance of the plugin (for example, if I am
> > performing an action on all <input>s, is there a way to store the
> > input type and reuse it later?). Maybe I'm not being clear, so
> > here's the example:
> >
> >                 $(".slider").each(function() {
> >                     rangeEl = $(this).siblings().filter("input.range");
>
> You haven't put the keyword "var" in front of rangeEl (and the others), so
> that creates a global variable; as you noted it's clobbered each time
> through the function. If you say "var rangeEl" it will be a local variable
> and you'll get a different one each time the function is called, creating a
> closure that your $(this).Slider() plugin can use.
>
> My experience is that 99 out of 100 times you want to say "var" anywhere you
> first start using the variable. Say it early and say it often--Javascript
> doesn't mind if you say it more than once in the same function. That
> especially applies to for-loop variables.  Every for loop should start with
> "for ( var" or it's most likely broken in a very dangerous way.
>
>
>
> _______________________________________________
> jQuery mailing list
> discuss@jquery.com
> http://jquery.com/discuss/
>


-- 
Ⓙⓐⓚⓔ - יעקב   ʝǡǩȩ   ᎫᎪᏦᎬ
▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒
░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░
▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒
░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░
▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒░▒
_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/

Reply via email to