Javascript 访问函数属性(.caller),而不指定它';她自己的名字
在JavaScript中,您可以访问函数的“函数属性”,例如Javascript 访问函数属性(.caller),而不指定它';她自己的名字,javascript,Javascript,在JavaScript中,您可以访问函数的“函数属性”,例如caller。(实际上,我甚至不知道“functional property”这个词是否正确。) 例如: func0 = function() { console.log(typeof func0.caller) } func1 = func0 func1() var o = { func2: func0, func3: function(){ console.log(typeof o.func3.caller) } } o.fu
caller
。(实际上,我甚至不知道“functional property”这个词是否正确。)
例如:
func0 = function() {
console.log(typeof func0.caller)
}
func1 = func0
func1()
var o = { func2: func0, func3: function(){ console.log(typeof o.func3.caller) } }
o.func2()
o.func3()
如您所见,在添加
.caller
之前,必须提供函数名。但是,如果函数是匿名的,或者出于某种原因,我不想使用该名称(也许我计划将来重命名该函数):我仍然可以访问调用者吗?您访问的是“分配”给每个函数的arguments对象。所以你不用函数名。您可以使用arguments对象
arguments对象的行为类似于数组,因此arguments[0]返回传递给函数的第一个参数
arguments.length
// a property of the arguments object that tells you how many arguments the function has
arguments.caller
// reference to the function that invoked the current function.
arguments.callee() will call the function recursively. Its a reference to the currently executing function.
这就是你的意思吗
Use arguments.callee.caller
这似乎是因为arguments.callee向您提供了对当前正在执行的函数的引用,而arguments.caller则引用了调用该函数的函数(实际上是同一个函数)。也许这就是为什么不建议使用arguments.caller
arguments.callee
将为您提供当前函数的引用。然后此代码段应该可以工作,但它会输出未定义的:func0=function(){console.log(typeof arguments.caller)};func0()
我认为这可能是因为调用方属性已过时。我认为你不应该依赖它。如果你需要当前函数的引用。然后使用arguments.Calleec你能更新你的答案吗,这样我就可以接受它是正确的吗?