文章标题: 发 表 人: 版块:
 
主题:关于JavaScript函数的超级问题?  发表时间: 2008-07-09 10:09
问题在代码注释中,谁能解惑呢?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>函数测试</title>
</head>

<body>
   <script>
       var f = function fact(x){
           if(x<=1) return 1 ;
           else return x * fact(x-1);
       };
       
       alert(f(3));
       alert(fact(3));//在火狐下该语句不能运行,报错:fact is not defined  这是为什么呢?
   </script>
</body>
</html>
发表时间: 2008-07-09 10:09
        [引用回复]
本来就没有定义的吗? fact都不存在。在 alert(fact(3)); 的时候, 环境中只存在f函数
发言人:woweiwokuang VIP  楼层:1
发表时间: 2008-07-09 11:10
        [引用回复]
你在仔细看看代码啊!function fact(x){
已经定义过了
发言人:yangyong0614  楼层:2
发表时间: 2008-07-09 11:17
        [引用回复]
var f = function fact(x){
          if(x<=1) return 1 ;
          else return x * fact(x-1);
      };
你可以看看。 上面的内容是在函数内部定义的,而是直接赋值给 f 变量。
如果这样写你试试:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>函数测试</title>
</head>

<body>
  <script>
function fact(x){
          if(x<=1) return 1 ;
          else return x * fact(x-1);
      };
      var f = fact;
     
      alert(f(3));
      alert(fact(3));//在火狐下该语句不能运行,报错:fact is not defined  这是为什么呢?
  </script>
</body>
</html>
发言人:woweiwokuang VIP  楼层:3
发表时间: 2008-07-09 01:06
其它版块:


标题:关于JavaScript函数的超级问题?



论坛发言支持ubb格式(把HTML页面代码转化为UBB的工具) 添加/删除可视化编辑器
上传附件:
提示:您能上传的附件单个文件最大为2M
  
有新帖的版块
无新帖的版块

Copyright (C) 2008 vifir.com 开源人 渝ICP备06004507号 如有意见请与我们联系