arguments,callee&caller测试

关键字:arguments,callee,caller
arguments:表示传入函数的参数
callee:表示函数和函数主体的语句
caller:表示调用该函数的函数
arguments

该对象代表正在执行的函数和调用它的函数的参数。
caller
返回一个对函数的引用,该函数调用了当前函数。
functionName.caller
functionName 对象是所执行函数的名称。
说明
对于函数来说,caller属性只有在函数执行时才有定义。如果函数是由顶层调用的,那么 caller包含的就是 null 。如果在字符串上下文中使用 caller 属性,那么结果和functionName.toString一样,也就是说,显示的是函数的反编译文本。
callee

返回正被执行的 Function 对象,也就是所指定的Function 对象的正文。

[function.]arguments.callee
可选项 function 参数是当前正在执行的 Function 对象的名称。

说明

callee 属性的初始值就是正被执行的 Function 对象。

callee 属性是 arguments对象的一个成员,它表示对函数对象本身的引用,这有利于匿名
函数的递归或者保证函数的封装性,例如下边示例的递归计算1到n的自然数之和。而该属性
仅当相关函数正在执行时才可用。还有需要注意的是callee拥有length属性,这个属性有时候
用于验证还是比较好的。arguments.length是实参长度,arguments.callee.length是
形参长度,由此可以判断调用时形参长度是否和实参长度一致。
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
 
<head>
  
<metahttp-equiv="Content-Type" content="text/html; charset=gb2312"/>
  
<title>UntitledDocument</title>
  
<meta name="author"content="Sam Lin" />
 
<script type=text/javascript>
  
function testTag()
  {
   
var sTemp ="This is a function :testTag() start:\n\n Content:\n\n";
   sTemp
+=arguments.callee+ "\n\n";
   sTemp
+="Lengthof Arguments are :"+arguments.length+"\n";
   sTemp
+="TheArguments are :\n\n";
   
for(vari=0;i<arguments.length;i++)
    sTemp
+=arguments[i]+ "\n";
   sTemp
+="ClickOk to invoke a()\n";
   alert(sTemp);
   a(arguments); 
  }
  
function a()
  {
   
var sTemp ="The start of function a():\n\n";
   sTemp
+="Thecallee.caller:"+arguments.callee.caller;
   sTemp
+="Toclick OK to invoke b()";
   alert(sTemp);
   b(arguments);
  }
  
function b()
  {
   
var sTemp ="The start of function b():\n";
   sTemp
+="Thecallee.caller.caller:"+arguments.callee.caller.callee;
   alert(sTemp);
  }
 
</script> 
 
</head>
 
<body>
 
<input type=buttononclick="javascript:testTag('Sam','SamLin','Lucky','Success');"value="testTag('Sam,'SamLin','Lucky','Success')">
 
</body>
</html>
posted @ 2008-02-20 22:25  Sam Lin  阅读(578)  评论(1编辑  收藏  举报