概述
caller
返回一个对函数的引用,该函数调用了当前函数。
functionName.caller
functionName 对象是所执行函数的名称。
说明
对于函数来说,caller属性只有在函数执行时才有定义。如果函数是由顶层调用的,那么 caller包含的就是 null 。如果在字符串上下文中使用 caller属性,那么结果和 functionName.toString 一样,也就是说,显示的是函数的反编译文本,
注意:Function.toString() 可以实现 Function 的反编译功能.如加上递归功能则功能更加强大
例子一
<span style="font-size:18px;"><span style="font-size:18px;"><script type="text/javascript">
function callerdemo() {
if (callerdemo.caller) {
var a= callerdemo.caller.toString();
alert(a);
} else {
alert('this is a top function');
}
};
callerdemo();
</script>
</span></span>
例子一中由于callerdemo是由顶层调用,所以callerdemo.caller 包含的就是null,故而输出值是 “this is a top function”
例子二
<span style="font-size:18px;"><script type="text/javascript">
window.οnlοad=function(){
function callerdemo() {
if (callerdemo.caller) {
var a= callerdemo.caller.toString();
alert(a);
} else {
alert('this is a top function');
}
};
callerdemo();
}
</script></span>
function(){
function callerdemo() {
if (callerdemo.caller) {
var a= callerdemo.caller.toString();
alert(a);
} else {
alert('this is a top function');
}
};
callerdemo();
}
例子三
<span style="font-size:18px;"> function callerdemo() {
if (callerdemo.caller) {
var a= callerdemo.caller.toString();
alert(a);
} else {
alert('this is a top function');
}
};
function handlecaller() {
callerdemo();
};
handlecaller();</span>
例子三和例子二一样,都是一个对调用函数的应用,返回值是:
function handlecaller() {
callerdemo();
};
callee
返回正被执行的 Function 对象,也就是所指定的 Function 对象的正文。[function.]arguments.callee可选项 function参数是当前正在执行的 Function对象的名称。说明callee属性的初始值就是正被执行的 Function 对象。callee属性是 arguments 对象的一个成员,它表示对函数对象本身的引用,这有利于匿名函数的递归或者保证函数的封装性,例如下边示例的递归计算1到n的自然数之和。而该属性仅当相关函数正在执行时才可用。还有需要注意的是callee拥有length属性,这个属性有时候用于验证还是比较好的。arguments.length是实参长度,arguments.callee.length是形参长度,由此可以判断调用时形参长度是否和实参长度一致。
<span style="font-size:18px;"><script type="text/javascript">
function callerdemo() {
alert(arguments.callee);
};
callerdemo();
</script></span>
返回的即是callerdemo 函数本身。
<span style="font-size:18px;"> function callerdemo() {
alert(arguments.callee.length+","+arguments.length);
};
callerdemo(5);</span>
输出调用时形参长度和实参长度
<span style="font-size:18px;">function callerdemo(n) {
if(n==0) return false;
console.log(n);
callerdemo(n-1);
};
callerdemo(5);</span>
输出 5,4,3,2,1;
<span style="font-size:18px;"> function callerdemo(n) {
if(n==1) return 1;
return n*callerdemo(n-1);
};
console.log(callerdemo(5));</span>
输出120;
<span style="font-size:18px;"> function callerdemo(n) {
if(n==1) return 1;
return n*arguments.callee(n-1);
};
console.log(callerdemo(5));</span>
输出120;
最后
以上就是老迟到小天鹅为你收集整理的arguments的callee属性的全部内容,希望文章能够帮你解决arguments的callee属性所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复