callee caller

callee 属性

返回正被执行的 Function 对象,即指定的 Function 对象的正文。

[ function. ]arguments.callee

callee 属性是arguments 对象的一个成员,该属性仅当相关函数正在执行时才可用。

callee 属性的初始值时正在执行的Function 对象。这将永远函数匿名递归。

  (1)  function factorial(num) {
        if( num<=1){
            return 1;
        }else {
            return num * factorial(num-1);
        }
    }
    
  (2)  function factorial(num) {
        if(num<=1){
            return 1;
        }else {
            return num * arguments.callee(num-1);
        }
    }
    
    alert(factorial(5)); //120
(1)函数有函数名,而且名字以后不回改变的情况下,这样定义美绅问题。但函数的执行与函数名 factorial 紧紧耦合在一起,为了消除这种紧密耦合的现象,可以使用arguments.callee 

caller 属性

返回一对函数的引用,该函数调用了当前函数

function.caller

caller 属性只有当函数正在执行才被定义。如果函数时从JScrip 程序顶层背调用,则caller 包含 null

    function callLevel() {
        if(callLevel.caller == null){
            document.write("callLevel was called from the top level"+"<br/>");
        }else{
            document.write("callLevel was called by:");
            document.write(callLevel.caller);
        }
    }
    function testCall() {
        callLevel()
    };
    callLevel();//callLevel was called from the top level
    testCall();//callLevel was called by:function testCall() { callLevel() }

callee 结合 caller 

当caller 函数名使用不方便时候,可以用arguments.callee 代替函数

    function a() {
        b();
    }
    function b() {
        alert(arguments.callee.caller);
    }
    a();





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值