JScript的语法教程里面说在声明变量时忽略var要害字是完全合法的。但是事实经常又证实想当然的结果是不可靠的。
看看下面这几个例子的结果就知道问题了:
<script language="javascript">
var var00 = 0;
document.write(var00 + '<br>');
var var01 = 1;
function foo()
{
document.write(var01);
var var01 = 1;
}
foo();
</script>
<script language="Javascript">
var00 = 0;
document.write(var00 + '<br>');
var01 = 1;
function foo()
{
document.write(var01);
var01 = 1;
}
foo();
</script>
<script language="javascript">
var00 = 0;
document.write(var00 + '<br>');
var01 = 1;
function foo()
{
document.write(var01);
var var01 = 1;
}
foo();
</script>
这三个例子的执行结果分别是:
Results#region Results
No.1
0
undefined
No.2
0
1
No.3
0
undefined
#endregion
[1] [2] 下一页