日期:2014-05-16 浏览次数:20456 次
arguments, caller , callee 是什么?
在javascript 中有什么样的作用?本篇会对于此做一些基本介绍。
<!--by oscar999 2013-1-16-->
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Arguments Test</title>
</head>
<body>
<script>
function testArg()
{
alert("real parameter count: "+arguments.length);
for(var i = 0; i < arguments.length; i++)
{
alert(arguments[i]);
}
}
testArg(11); //count: 1
testArg('hello','world'); // count: 2
</script>
</body>
</html>看上去很简单。 需要注意的是 argument 保存的实参的信息。(function () {
alert(arguments instanceof Array); // false
alert(typeof(arguments)); // object
})();对于以上立即执行函数写法不清楚的话, 可以参考alert(new Function().arguments);//return null
<script>
function testCaller() {
var caller = testCaller.caller;
alert(caller);
}
function aCaller() {
testCaller();
}
aCaller();<script>
function aCallee(arg) {
alert(arguments.callee);
}
function aCaller(arg1, arg2) {aCallee();}
aCaller();
</script>