为何会产生以下结果差异,哪位大侠给个权威明白解释
(1)两段代码的比较:<script type="text/javascript">
f ( );
function f( ){
alert(1);
}
</script>程序运行的结果是:弹出数字“1”<script type="text/javascript">
f ( );
var f=function ( ){
alert(1);
}
</script>程序运行会发生错误,缺少对象。。 (2)再来段代码比较:<script type="text/javascript">
function hello( ){
alert("hello");
}
hello( );
function hello( ){
alert("hello world");
}
hello( );
</script>连续弹出2个hello world,而非先hello,后hello world
<script type="text/javascript">
function hello( ){
alert("hello");
}
hello( );
var hello=function( ){
alert("hello world");
}
hello( );
</script>连续先弹出hello,后弹出hello world <script type="text/javascript">
var hello=function ( ){
alert("hello");
}
hello( );
function hello( ){
alert("hello world");
}
hello( );
</script>连续弹出2个hello
(1)两段代码的比较:<script type="text/javascript">
f ( );
function f( ){
alert(1);
}
</script>程序运行的结果是:弹出数字“1”<script type="text/javascript">
f ( );
var f=function ( ){
alert(1);
}
</script>程序运行会发生错误,缺少对象。。 (2)再来段代码比较:<script type="text/javascript">
function hello( ){
alert("hello");
}
hello( );
function hello( ){
alert("hello world");
}
hello( );
</script>连续弹出2个hello world,而非先hello,后hello world
<script type="text/javascript">
function hello( ){
alert("hello");
}
hello( );
var hello=function( ){
alert("hello world");
}
hello( );
</script>连续先弹出hello,后弹出hello world <script type="text/javascript">
var hello=function ( ){
alert("hello");
}
hello( );
function hello( ){
alert("hello world");
}
hello( );
</script>连续弹出2个hello
你来分析下 这个的结果
<script type="text/javascript">
var hello=function ( ){
alert("hello");
}
hello( );
function hello( ){
alert("hello world");
}
hello( );
</script>
第一步扫描var关键字,提前到最顶端:
var hello;
第二步扫描function定义式,提到var之后:
var hello;
function hello(){
alert('hello world');
}
第三步顺序执行代码:最终如下:
var hello;
function hello(){
alert('hello world');
}
hello = function(){
alert('hello');
}
hello(); // 弹hello
hello();//弹hello
分全部给你。