最近在复习javascript的事件处理时发现了一个问题,然后也是我来写javascript的预编译和执行顺序的问题
代码:
代码一
<html>
<head>
<title>事件处理</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
<script type='text/javascript'>
//页面在在完成加载后
window.onload=function(){
var input=document.getElementById('button');
var p=document.getElementById('p');
var i=1;
while(input){
input.onclick=function(){
p.innerHTML+='<br />('+ i +') '+this.nodeName;
}
i++;
input=input.parentNode;
}
}
</script>
</head>
<body>
<div>
<input type='button' value='Event事件' id='button' />
<p id='p'>事件捕获的顺序:</p>
</div>
</body>
</html>
显示的结果为:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAT0AAADCCAIAAAC0SoKyAAAIkklEQVR4nO3dy27USBTGcT9YSIBFYI+QkHgDXoI3QETiDZAQmh1bYIFIxAZCwmgIiMsDwMxoCJdFYNGziIgsV9U5p8rV3XXs/08t5LaPq9xRf13tW9P98fiABw8evh7dAoA35Bbwh9wC/pBbwB9yC/hDbgF/yC3gD7kF/CG3gD9Kbu8/+3Bt5/HGzQdzeFzbeXz/2YfV/N2BMaTc3tt7v3Xn6fbz48tvf83hsf38eOvO03t771f21wfKSLm9evvR9ovjGx9Pvs7DjY8n2y+Or95+tLK/PlBGyu3GzQeX3/xad5pW6vKbXxs3H6zsrw+U0XN73KSu6+xL5eI+cgsX9Nx+aUOnGRSf/dufUJFbuKDk9tLRz//SwvAIxVWcdRFOZNWkXDr6SW7RPj23/4q6rotOj5RqKjrSpta1FA+QW7ig5Hb76OQfUdd1ckEZtdlUwWk4BwX2jdw+OiG3aJ+W29cnf4u6rgsnTg2mU0/7E/2a/szoIqEstZGq7dfkFg4oub346sdHUT88/ZnhdGqmXCD0myoO11VbO3Px1Q9yi/Ypub1w+P29qOu6wURqejBCGtfqz5EN1lXLoi4cfie3aJ+e23eiruuE+f2l0cpUQapZS3G4rtraGXILF5Tcnj/89lbUdZ0wLS+1FAz6SkltktBa1PnDb+QW7dNye/DtKC3Mz2Bpql54OigWurYv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yC3gD7kF/CG3gD/kFvCH3AL+kFvAH3IL+ENuAX/ILeAPuQX8IbeAP+QW8IfcAv6QW8Afcgv48z8Q2BJTygOeLwAAAABJRU5ErkJggg==" alt="">
当我更改了代码中红色的部分后得到的结果又不相同:
代码二
<html>
<head>
<title>事件处理</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
<script type='text/javascript'>
//页面在在完成加载后
window.onload=function(){
var input=document.getElementById('button');
var p=document.getElementById('p');
var i=1;
while(input){
input.onclick=function(){
p.innerHTML+='<br />('+ i++ +') '+this.nodeName;
}
input=input.parentNode;
}
}
</script>
</head>
<body>
<div>
<input type='button' value='Event事件' id='button' />
<p id='p'>事件捕获的顺序:</p>
</div>
</body>
</html>
得到的结果为:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAToAAACoCAIAAAD/+bX3AAAIOElEQVR4nO3dT27USBTHcR8sJMAisEdISNyAS3ADRCRugITYsgUWiERsIBBGQ0D8OUCYGQ3hzyKw8CzQtCxXvVevqtzd9drfj9DIbb+qckf963Lb7p6uB+BEt+4dAGBFXAE3iCvgBnEF3CCugBvEFXCDuAJuaHG9/+zjtb3HWzcfzOHftb3H9599XNnfHSggxvXewYedO093n59efvdrDv92n5/u3Hl67+DDKv/6QBYxrldvP9p9cXrj09nXebjx6Wz3xenV249W+dcHsohx3br54PLbX+sO0Updfvtr6+aDVf71gSyJuJ42qes6+1a9eIi4onGJuH5pQ5cyKl78d7iQRFzROC2ul45//isLM6MUT2IxRLiQVSO5dPyTuKJlibj+o+q6LrpcSeoqOq9KbS3FI8QVjdPiunt89req6zq9oEyyW6ngdyZHBfad3D0+I65omRrXN2d/qbquCxd+Gy1LD4cLw5rhyugmpUzayaTdN8QVTdPievH1j0+qYWaGK8NlaaVeoIwrFYdtk70tXHz9g7iiZVpcLxx9/6Dqum60IC2P5kNjq+Ea3ahtsizqwtF34oqWJeL6XtV1nbJ+uDVaKRVI3VqKw7bJ3haIKxqnxfX80bd3qq7rlGV9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D5hftwlc0ukQAAAABJRU5ErkJggg==" alt="">
得出这两种不同的结果那是因为javascript代码在运行时有预编译和执行两个阶段,在预编译阶段会对函数和变量进行处理,对所有的声明变量会赋值为underfined,对所有的声明函数也会赋值为函数的定义。
下面我们来测试javascript的执行过程
1.javascript代码执行顺序时按照脚本标签<script>出现的顺序来确定的,浏览下面页面你会发现代码是按从上到下的顺序执行的
<script type='text/javascript'>
alert('one');
</script>
<script type='text/javascript'>
alert('two');
</script>
<script type='text/javascript'>
alert('three');
</script>
2. 因为变量在预编译时被赋予一个undefined初值,所以下面代码中,第一个变量name在代码中没有被赋值,所有就延用undefined这个值,下面的name被赋予了Jude,所以第二次输出的是Jude这个字符。
<script type='text/javascript'>
alert(name); //显示undefined
var name='Jude';
alert(name); //显示Jude
</script>
3.从如下结果中我们知道先是连续两次输出Hello Wrold!,最后连续两次输出test,得出这样的结果是因为javascript并非是完全按照顺序执行的,而是在执行之前先进行一个预编译,预编译时声明式函数被提取出来,优先执行,而且相同的函数会进行覆盖,再执行赋值式函数。
<script type='text/javascript'>
test(); //输出Hello World!
function test(){
alert('hello'); //声明式函数
}
test(); //输出Hello World! var test=function(){ //赋值式函数
alert('test');
}
test(); //输出test
function test(){ //声明式函数
alert('Hello World!');
}
test(); //输出test
</script>
4.下面代码显示显示hello,再显示hello world!,这是因为javascript中的给个代码块是相互独立的,当脚本遇到第一个<script>标签时,则javascript解析器会等这个代码块加载完成后,先对它进行预编译,然后再执行之,然后javascript解析器准备解析下一个代码块,由于javascript是按块执行的,所有一个javascript调用下一个块的函数或者变量时,会出现错误
<script type='text/javascript'>
function test(){
alert('hello'); //显示hello
}
test()
</script>
<script type='text/javascript'>
function test(){
alert('hello world!'); //显示hello world!
}
test()
</script>
5.虽然javascript是按块执行的,但不同的块却属于相同的全局作用域,不同的块的变量和函数式可以相互使用的,也就是某个块可以使用前面块的变量和函数,却不可以使用它之后的块的变量和函数
<script type='text/javascript'>
alert(name); //显示undefined
var name='Jude';
function test(){
alert('hello');
}
fun(); //不能调用下一个块的函数
</script>
<script type='text/javascript'>
alert(name); //可以调用上一个块的变量,显示Jude
test(); //可以调用上一个块的函数,显示hello
function fun(){
alert('fun');
}
</script>
6.javascript在预编译阶段是以函数来划分作用域的,然后再通过var 声明的变量来与声明函数开辟内存空间,对var变量赋初值undefined。在执行阶段再根据作用域来嘴变量进行赋值.
第一个代码块中函数里面的变量a是局部变量,因为a在函数内重新用var定义,所以输出undefined,而变量b是全局变量,因为在函数内没有用var重新声明b,所以在给变量b赋值时到全局变量中找全局变量b的值,所以输出的是b.
第二个代码块中的函数内都重新声明了变量a和b,所以他们都是函数内的局部变量,所以都输出undefined。
<script type='text/javascript'>
var a='a';
var b='b';
function test(){
alert(a); //显示undefined
alert(b); //显示b
var a='test';
}
test();
</script>
<script type='text/javascript'>
var a='a';
var b='b';
function test(){
alert(a); //显示undefined
alert(b); //显示undefined
var a='test';
var b='test';
}
test();
</script>
综上所述,javascript在执行时的步骤是:
1、先读入第一段代码块
2、对代码块进行语法分析,如果出现语法错误,直接执行第5步骤
3、对var变量和function定义的函数进行“预编译处理”(赋值式函数是不会进行预编译处理的)
4、执行代码块,有错则报错
5、如果还有下一段代码块,则读入下一段代码块,重复步骤2
6、结束