我不明白为什么在函数内部声明变量时会表现得如此奇怪。
在
first函数中,我使用let变量b和c值10进行声明:b = c = 10;在
second函数中,我显示:b + ", " + c这表明:
10, 10同样在
first函数中,我声明a为10:let a = b = c = 10;但是在
second函数中它显示了一个错误:找不到变量:
a现在在
first函数中,我声明d为20:var d = 20;但是在
second函数中,它显示出与以前相同的错误,但带有变量d:找不到变量:
d
例:
function first() {
let a = b = c = 10;
var d = 20;
second();
}
function second() {
console.log(b + ", " + c); //shows "10, 10"
try{ console.log(a); } // Rreference error
catch(e){ console.error(e.message) }
try{ console.log(d); } // Reference error
catch(e){ console.error(e.message) }
}
first()
Dim Apple, Banana, Pear As Fruit是手段Dim Apple / Dim Banana / Dim Pear As Fruit,而不是Dim Apple As Fruit / ...。
b和c没有前缀var关键字。a并且d是本地的first。