【发布时间】:2015-11-25 15:33:29
【问题描述】:
根据What's the difference between using "let" and "var" to declare a variable?,当在 for 循环中使用时,let 关键字的作用域比 var 小。这是否意味着在所有 'for (var i=0...' ...' 希望 var i 在 for 循环之外仍然可见,这意味着所有 'for (var i=0...' 都是错误的,正确的方法是 'for (let i=0... '?只是一个是或否的问题。
function allyIlliterate() {
//tuce is *not* visible out here
for( let tuce = 0; tuce < 5; tuce++ ) {
//tuce is only visible in here (and in the for() parentheses)
};
//tuce is *not* visible out here
};
function byE40() {
//nish *is* visible out here
for( var nish = 0; nish < 5; nish++ ) {
//nish is visible to the whole function
};
//nish *is* visible out here
};
【问题讨论】:
-
从答案的完全分裂来看,有充分的理由支持和反对,我认为这可能符合“主要基于意见”。
-
@ajm 我基本同意,尽管措辞清楚地询问“所有 for 循环”是否“错误”,我认为这不是意见问题。
-
确实如此。看看答案是否保持相同的 50/50 分割会很有趣。 :-)
标签: javascript