arguments.callee与arguments.callee.caller

arguments.callee

如下示例:

function parent1() {
    console.log(arguments.callee);
}

parent1(1,2,3);

运行结果为:

function parent1() {
    console.log(arguments.callee);
}

由此可见,arguments.callee是指向参数arguments对象的函数,在此处就是parent1()函数了。所以说arguments.callee的意思也就好理解了,calleearguments对象的一个成员,它的值为“正被执行的Function对象”。

arguments.callee.caller

caller是函数的一个属性,该属性保存着调用当前函数的函数。

看概念还是有点不好理解,直接看一个例子

function parent2() {
    function child() {
        console.log(arguments.callee.caller)
    }
    child()
}

parent2()

由上述介绍的argument.callee指向参数arguments对象的函数,所以这里就表示child()这个函数,所以,这里也可以这样写

function parent2() {
    function child() {
        console.log(child.caller)
    }
    child()
}

parent2()

打印的结果是一样的,运行结果:

function parent2() {
    function child() {
        console.log(arguments.callee.caller)
    }
    child()
}

就是说child的调用者是parent2,如果这个父调用者不存在呢,那么它将会返回null

function child() {
    console.log(arguments.callee.caller)
}
child()

所以说上面的这个运行后的结果是null,因为其不存在父调用者。

如果您觉得本文对您有用,欢迎捐赠或留言~
微信支付
支付宝

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注