在以下代码中:
function outer() {
var x = 'foo';
function inner() {
var y = x; // y == 'foo'
var x = 'bar'; // x == 'bar', y == undefined
}
}
为什么变量y
在inner()
内未定义?它不应该引用x
中的outer()
吗?
如果删除了行var x = 'bar';
,则y
确实具有值'foo'。
答案 0 :(得分:2)
int a = 5;
auto *somePtr = 5;
std::cout << typeid(*somePtr).name() << std::endl;
函数被解释为它是这样编写的:
Sub TEST_SUB(surface)
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
Worksheets("Sheet3").Activate
ActiveSheet.DisplayPageBreaks = False
Sheets("Sheet3").Range("A4:Z400").ClearContents
y = 4 'y is the row on sheet3 where we want to paste
For x = 4 To 20 'x is the current row from which we want to copy
' Decide if to copy based on whether the value in col 10 matches the parameter Surface
ThisValue = Sheets("Tests_Master").Cells(x, 10).Value
If ThisValue = surface Or x = 4 Then
R1 = "A" + CStr(x) + ":K" + CStr(x) 'Range to copy from: row X columns 1-10
'This next statement taks about 2 seconds to execute ! WHY????
Sheets("Tests_Master").Range(R1).Copy Destination:=Sheets("sheet3").Range("A" + CStr(y))
y = y + 1
End If
Next x
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
声明已悬挂,但初始化从上到下进行处理。因此,在整个inner
函数中,符号 function inner() {
var y;
var x;
y = x; // y == undefined
x = 'bar'; // x == 'bar'
}
和inner
都引用该函数中声明为 local 的变量;特别是x
是本地y
,而不是封闭上下文中的x
。因此,当评估x
的初始化表达式时,y
是本地x
,尚未初始化; <{1>}的初始化程序后,的初始值设定项表达式为。