js函数 test.caller 谁在调用test函数

时间:2023-03-09 06:08:37
js函数 test.caller 谁在调用test函数

返回调用指定函数的函数.


function test() {
if (test.caller === null)
console.log('test 函数在全局调用'); // 获取调用 test函数, 的函数名
console.log(test.caller.name );
// 更上面一样
console.log( arguments.callee.caller.name ); // 获取 test函数的auguments
console.log( Array.prototype.slice.call(arguments));
// 获取 调用test函数,的函数的 arguments
console.log( Array.prototype.slice.call(arguments.callee.caller.arguments));
} function a(arg1, arg2) {
test(1)
} function b() {
test(2)
}
a(123)
b() function test2 (n) {
if(n <=0){
return null
}
// 判断 函数是否递归
console.log(
test2.caller &&
test2.caller.name === 'test2'
? '递归'
: test2.caller && test2.caller.name
);
return test2(n - 1)
} test2(3)