我可以在JavaScript的不同for循环中声明两次相同的变量吗? [英] Can I declare the same variable twice in different for loops in JavaScript?

查看:144
本文介绍了我可以在JavaScript的不同for循环中声明两次相同的变量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

JavaScript可变范围

我有一个用于HTML选择选项的JavaScript函数:

I have a JavaScript function for HTML select options for days:

// Show and hide days according to the selected year and month.
function show_and_hide_days(fp_form) {
    var select_year= $(fp_form).find("select.value_year");
    var select_month= $(fp_form).find("select.value_month");
    var select_day= $(fp_form).find("select.value_day");
    var selected_year= $.parse_int($(select_year).val());
    var selected_month= $.parse_int($(select_month).val());
    var selected_day= $.parse_int($(select_day).val());
    var days_in_month= new Date(selected_year, selected_month, 0).getDate();
    // If the number of days in the selected month is less than 28, change it to 31.
    if (!(days_in_month >= 28))
    {
        days_in_month= 31;
    }
    // If the selected day is bigger than the number of days in the selected month, reduce it to the last day in this month.
    if (selected_day > days_in_month)
    {
        selected_day= days_in_month;
    }
    // Remove days 29 to 31, then append days 29 to days_in_month.
    for (var day= 31; day >= 29; day--)
    {
        $(select_day).find("option[value='" + day + "']").remove();
    }
    for (var day= 29; day <= days_in_month; day++)
    {
        $(select_day).append("<option value=\"" + day + "\">" + day + "</option>");
    }
    // Restore the selected day.
    $(select_day).val(selected_day);
}

我的问题是 - 我可以在两个不同的地方声明var day两次循环和这个变量的范围是什么?这是合法的,如果我在同一个函数中声明两次相同的变量会发生什么? (内部循环或外部循环)?例如,如果我用var再次声明其中一个变量会发生什么?

My question is - can I declare "var day" twice in two different for loops and what is the scope of this variable? Is it legal and what happens if I declare the same variable twice in the same function? (inside for loops or outside for loops)? For example, what happens if I declare one of the variables again with "var"?

如果我在for循环中的变量日之前根本不使用var ,会发生什么?

If I don't use "var" at all before variable day in for loops, what will happen?

谢谢,
Uri。

Thanks, Uri.

PS $ .parse_int是一个jQuery插件,如果没有指定,它会调用基数为10的parseInt。

P.S. $.parse_int is a jQuery plugin which calls parseInt with radix 10 if not specified.

推荐答案

使用 var foo 将 foo 范围扩展到该函数。由于 var 声明被挂起,因此在函数中的位置并不重要。

Any use of var foo in a function will scope foo to that function. It doesn't matter where in the function this takes place as var declarations are hoisted.

<的其他用途同一函数中的code> var foo 在语法上是合法的,但由于变量已经作用于该函数,因此无效。

Additional uses of var foo in the same function are syntactically legal but will have no effect as the variable is already scoped to that function.

因为它没有效果,所以有一种思想建议反对它(并且支持在函数的最顶部的单个 var 函数来执行所有范围)避免暗示它具有重要性(对于不完全熟悉JavaScript的这一特性的维护者)。 JSLint 会提醒您使用此功能。

Since it has no effect, there is a school of thought that recommends against it (and in favour of a single var function at the very top of a function to perform all the scoping) to avoid implying that there is significance to it (to maintainers who are not entirely comfortable with this feature of JavaScript). JSLint will alert you to this usage.

这篇关于我可以在JavaScript的不同for循环中声明两次相同的变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆