为何会产生以下结果差异,哪位大侠给个权威明白解释
(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.   

    函数表达式和函数定义式先搞清楚:函数表达式就是: var a = function(){}; 函数定义式就是 function a(){};JS的解析方式是,对var关键字先提前声明,接着对函数定义式进行提前加在var后头,再接着顺序执行代码,你按着这个去分析就行了。
      

  2.   

    @dxx1988
    你来分析下 这个的结果
    <script type="text/javascript">
    var hello=function (  ){
    alert("hello");
    }
    hello(  );
    function hello(  ){
    alert("hello world");
    }
    hello(  );
      </script>
      

  3.   


    第一步扫描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
      

  4.   

    dxx1988 牛比闪闪。~
    分全部给你。