我通过
获取来电者功能的信息arguments.callee.caller
但是,如果我想再次调用调用函数,我必须做什么?
答案 0 :(得分:3)
只需再次调用该功能。
arguments.callee.caller()
示例:
function A(){
B();
}
function B(){
arguments.callee.caller(); // It will call the A again.
}
答案 1 :(得分:2)
函数arguments.callee.caller
内部是对调用函数的引用,实际上是typeof arguments.callee.caller === 'function'
所以你可以直接调用它:
arguments.callee.caller(arg1, arg2, arg3, [...]);
或者你可以这样做:
arguments.callee.caller.call(context, arg1, arg2, arg3, [...]);
或者这个:
arguments.callee.caller.apply(context, [arg1, arg2, arg3, [...]]);
正如其他人所说,请注意性能命中!
答案 2 :(得分:0)
我的第一个提示是:
var nm = arguments.callee.caller.name
然后调用“nm”。使用eval或一些switch-cases。
答案 3 :(得分:0)
注意无限循环:
// The 'callee'
function f(arg) {
var cf = arguments.callee.caller;
cf.call(42);
}
// .. and the caller
function g(arg) {
if (arg === 42) {
alert("The Answer to..");
} else {
f(1); // or whatever
}
}
// call the caller
g(21)
答案 4 :(得分:0)
你应该支持Function.caller而不是arguments.callee.caller(尤其是因为人们无法弄清楚这是否被弃用)
Why was the arguments.callee.caller property deprecated in JavaScript?
举例说明用法:
var i = 0;
function foo () {
bar();
}
function bar() {
if ( i < 10 ) {
i += 1;
bar.caller();
}
}
foo();
// Logs 10
console.log(i);
虽然在现实世界中,您可能希望在调用之前检查调用者是否为函数。