首先看一下几段代码输出不同的结果
<script type="text/javascript">
var myfun = function(){
alert("hello world");
}
myfun();
//alert(typeof(myfun));
myfun = function(){
alert("hello myfun");
}
myfun();
</script>
<script type="text/javascript">
function myfun(){
alert("hello world");
}
myfun();
function myfun(){
alert("hello myfun2");
}
myfun();
</script>
第一个先输出hello world后输出hello myfun,变量被覆盖
第二个两次输出hello myfun2 原因是javascript的代码执行时一段一段执行的。
如果是这样写就会和第一个相同输出了<script type="text/javascript"> function myfun(){ alert("hello world"); } myfun();
</script>
function myfun(){alert("hello myfun2");}myfun();</script><script type="text/javascript">