Can I declare the same variable twice in different for loops in JavaScript? [duplicate]

点点圈 提交于 2019-11-28 02:25:09

问题


Possible Duplicate:
JavaScript Variable Scope

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);
}

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"?

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

Thanks, Uri.

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


回答1:


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.

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.

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.




回答2:


No you shouldn't. JavaScript has function scope, not block scope!

I'm saying shouldn't because it might suggest that the variable is local to the loop/block when it isn't.



来源:https://stackoverflow.com/questions/14010154/can-i-declare-the-same-variable-twice-in-different-for-loops-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!