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
的意思也就好理解了,callee
是arguments
对象的一个成员,它的值为“正被执行的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
,因为其不存在父调用者。
如果您觉得本文对您有用,欢迎捐赠或留言~
- 本博客所有文章除特别声明外,均可转载和分享,转载请注明出处!
- 本文地址:https://www.leevii.com/?p=925