arguments.callee

This property is a reference to the function that you are in. Which is very handy if you want to get a reference to an anonymous function from inside itself. One good reason for doing this would be in a recursive anonymous function.

arguments.caller

This property is a reference to the function that called the function you are in. This allows you to get access to the entire current callstack which is very handy for debugging.


function test() {
    console.log(">>>test");
    console.log("arguments.callee\n" + arguments.callee);
    console.log("arguments.caller\n" + arguments.caller);
    console.log("arguments.callee.caller\n" + arguments.callee.caller);
    test1();
}

function test1() {
    console.log(">>>test1");
    console.log("arguments.callee\n" + arguments.callee);
    console.log("arguments.caller\n" + arguments.caller);
    console.log("arguments.callee.caller\n" + arguments.callee.caller);        
}    
test();

//  로그기록
//>>>test
//arguments.callee 
//function test() { 
//    console.log(">>>test"); 
//    console.log("arguments.callee\n" + arguments.callee); 
//    console.log("arguments.caller\n" + arguments.caller); 
//    console.log("arguments.callee.caller\n" + arguments.callee.caller); 
//    test1(); 
//}
//arguments.caller undefined
//arguments.callee.caller null
//>>>test1
//arguments.callee 
//function test1() { 
//    console.log(">>>test1"); 
//    console.log("arguments.callee\n" + arguments.callee); 
//    console.log("arguments.caller\n" + arguments.caller); 
//    console.log("arguments.callee.caller\n" + arguments.callee.caller); 
//}
//arguments.caller undefined
//arguments.callee.caller 
//function test() { 
//    console.log(">>>test"); 
//    console.log("arguments.callee\n" + arguments.callee); 
//    console.log("arguments.caller\n" + arguments.caller); 
//    console.log("arguments.callee.caller\n" + arguments.callee.caller); 
//    test1(); 
//}

위와 같이 오출하면 로그(firefox)이 찍힌다.
결국 arguments.callee 는 함수의 입장에서 자기자신
arguments.callee.caller 는 arguments.callee를 호출한 함수이다.
호출한 쪽이 함수가 아니라 전역환경이라면
arguments.callee.caller null 이다.
arguments.callee는 익명함수의 재귀호출에 유용하다.

* 추가적으로 callee, caller가 null이거나 undefined가 아닐때
Object.prototype.toString.call(arguments.callee);
Object.prototype.toString.call(arguments.callee.caller);
호출해 보면은 둘다 [object Function]을 리턴하는것을 알수 있다.


posted by 뚱2