(1)
    var myfunc = function ()
        {
            alert("hello");
        };
    myfunc(); //第一次调用myfunc,输出hello
    
    myfunc = function ()
        {
            alert("yeah");
        };    
    myfunc(); //第二次调用myfunc,将输出yeah(2)function myfunc ()
    {
        alert("hello");
    };
    myfunc(); //这里调用myfunc,输出yeah而不是hello
    
    function myfunc ()
    {
        alert("yeah");
    };    
    myfunc(); //这里调用myfunc,当然输出yeah这两段代码不是一样的吗?怎么输出的结果会不一样?

解决方案 »

  1.   

    js 是预编译的.
    第1个: var 先声明了 函数 myfunc  后面是重新赋值
    第2个: 两个函数名相同 则后面的覆盖前面的(相当于 重写). alert 的肯定是后面的.
      

  2.   

    js 是预编译的.
    第1个: var 先声明了 函数 myfunc  后面是重新赋值
    第2个: 两个函数名相同 则后面的覆盖前面的(相当于 重写). 肯定是调用后面的.
      

  3.   

    好像是说JS是一段段执行的。定义式的函数语句会被优先执行,然后才执行其他语句。
    还有就是如果把2拆分开来。
    <script language="javascript">
      function myfunc()
      {
      alert("hello");
      };
      myfunc(); //第一次调用myfunc,输出hello
    </script>
    <script language="javascript">
      function myfunc()
      {
      alert("yeah");
      };  
      myfunc(); //第二次调用myfunc,将输出yeah
      </script>
    这样就正常了。
      

  4.   

    刚又去确认了下。应该是定义式的函数语句有优先权。。顺便GOOGLE了下。
    http://tieba.baidu.com/f?kz=638535203
    比你那个要全一点,可以参考
      

  5.   

    最主要的是HTML读代码是逐条读写的!~
    所以它按你定义的顺序显示!~
      

  6.   

    再说的明白点,等于第一段的执行顺序是
    var myfunc = function ()
      {
      alert("hello");
      };
      myfunc(); //第一次调用myfunc,输出hello
       
      myfunc = function ()
      {
      alert("yeah");
      };  
      myfunc(); //第二次调用myfunc,将输出yeah第二段是
    function myfunc ()
      {
      alert("hello");
      };
    function myfunc ()
      {
      alert("yeah");
      };
    myfunc();
    myfunc();我个人是这么理解的,不知道对不对。。
      

  7.   

    这样懂吗? 声明是优先于调用的!
    <script>
      var x;
      alert(x); //undefined
      x = 1 ;
      alert(x); //1  fun(); //报错? 不会!
      function fun()
      {
        alert("hello");
      }
    </script>
      

  8.   

    myfunc = function ()
      {
      alert("yeah");
      };   
     這樣寫就改變了函數體