javascript中arguments,callee,caller
在IE中,可以通过event或window.event获得全局event;而在其他浏览器中,则通过 arguments[arguments.length-1]可获得event实例。但是如果是嵌套调用,如:用户触发事件调用functionA,而 functionA又调用functionB,再由functionB调用functionC,此时如果functionB或functionC中需要获 得event实例,IE仍可通过event或window.event获得,而其他浏览器通过arguments[arguments.length- 1]就无法获得了,通常习惯的做法是把event作为参数传递到functionB及functionC中.
<!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>
<head>
<title>关于arguments,callee,caller等的测试 </title>
<meta http-equiv="content-type" content="text/html;" />
</head>
<body>
<script type="text/javascript">
function testArg() {
var sTemp = "test()开始执行\n\n函数定义的正文:\n\n";
sTemp += arguments.callee + "\n\n";
sTemp += "传入参数的长度:\n";
sTemp += arguments.length + "\n\n";
sTemp += "传入参数的内容:\n";
for (var i = 0; i < arguments.length; i++) {
sTemp += arguments[i] + "\n";
}
sTemp += "\n点击确定,开始调用a();";
alert(sTemp);
a(arguments);
}
function a(args) {
var sTemp = "a()开始执行\n\n函数定义的正文:\n\n";
sTemp += arguments.callee + "\n\n";
sTemp += "传入参数的长度:\n";
sTemp += arguments.length + "\n\n";
sTemp += "传入参数的内容:\n";
for (var i = 0; i < arguments.length; i++) {
sTemp += arguments[i] + "\n";
}
sTemp += "\n当前函数的调用者:\n";
sTemp += arguments.callee.caller + "\n\n";
sTemp += "\n点击确定,开始调用b();";
alert(sTemp);
b(args);
}
function b(args) {
var sTemp = "b()开始执行\n\n函数定义的正文:\n\n";
sTemp += arguments.callee + "\n\n";
sTemp += "传入参数的长度:\n";
sTemp += arguments.length + "\n\n";
sTemp += "传入参数的内容:\n";
for (var i = 0; i < arguments.length; i++) {
sTemp += arguments[i] + "\n";
}
sTemp += "\n当前函数的调用者:\n";
sTemp += arguments.callee.caller + "\n\n";
sTemp += "\n点击确定,开始调用c();";
alert(sTemp);
c(args);
}
function c(args) {
var sTemp = "c()开始执行\n\n函数定义的正文:\n\n";
sTemp += arguments.callee + "\n\n";
sTemp += "传入参数的长度:\n";
sTemp += arguments.length + "\n\n";
sTemp += "传入参数的内容:\n";
for (var i = 0; i < arguments.length; i++) {
sTemp += arguments[i] + "\n";
}
sTemp += "\n当前函数的调用者:\n";
sTemp += arguments.callee.caller + "\n\n";
alert(sTemp);
alert("args.callee:\n" + args.callee);
alert("args.callee.caller:\n" + args.callee.caller);
alert("testArg.arguments.callee.caller:\n" + testArg.arguments.callee.caller);
}
;
</script>
<input type="button" onclick="testArg('iecn.net','深蓝蝴蝶','枫岩');" value="testArg('iecn.net','深蓝蝴蝶','枫岩');" />
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<head>
<title>关于arguments,callee,caller等的测试 </title>
<meta http-equiv="content-type" content="text/html;" />
</head>
<body>
<script type="text/javascript">
function testArg() {
var sTemp = "test()开始执行\n\n函数定义的正文:\n\n";
sTemp += arguments.callee + "\n\n";
sTemp += "传入参数的长度:\n";
sTemp += arguments.length + "\n\n";
sTemp += "传入参数的内容:\n";
for (var i = 0; i < arguments.length; i++) {
sTemp += arguments[i] + "\n";
}
sTemp += "\n点击确定,开始调用a();";
alert(sTemp);
a(arguments);
}
function a(args) {
var sTemp = "a()开始执行\n\n函数定义的正文:\n\n";
sTemp += arguments.callee + "\n\n";
sTemp += "传入参数的长度:\n";
sTemp += arguments.length + "\n\n";
sTemp += "传入参数的内容:\n";
for (var i = 0; i < arguments.length; i++) {
sTemp += arguments[i] + "\n";
}
sTemp += "\n当前函数的调用者:\n";
sTemp += arguments.callee.caller + "\n\n";
sTemp += "\n点击确定,开始调用b();";
alert(sTemp);
b(args);
}
function b(args) {
var sTemp = "b()开始执行\n\n函数定义的正文:\n\n";
sTemp += arguments.callee + "\n\n";
sTemp += "传入参数的长度:\n";
sTemp += arguments.length + "\n\n";
sTemp += "传入参数的内容:\n";
for (var i = 0; i < arguments.length; i++) {
sTemp += arguments[i] + "\n";
}
sTemp += "\n当前函数的调用者:\n";
sTemp += arguments.callee.caller + "\n\n";
sTemp += "\n点击确定,开始调用c();";
alert(sTemp);
c(args);
}
function c(args) {
var sTemp = "c()开始执行\n\n函数定义的正文:\n\n";
sTemp += arguments.callee + "\n\n";
sTemp += "传入参数的长度:\n";
sTemp += arguments.length + "\n\n";
sTemp += "传入参数的内容:\n";
for (var i = 0; i < arguments.length; i++) {
sTemp += arguments[i] + "\n";
}
sTemp += "\n当前函数的调用者:\n";
sTemp += arguments.callee.caller + "\n\n";
alert(sTemp);
alert("args.callee:\n" + args.callee);
alert("args.callee.caller:\n" + args.callee.caller);
alert("testArg.arguments.callee.caller:\n" + testArg.arguments.callee.caller);
}
;
</script>
<input type="button" onclick="testArg('iecn.net','深蓝蝴蝶','枫岩');" value="testArg('iecn.net','深蓝蝴蝶','枫岩');" />
</body>
</html>