Each function in js has a "caller" property, from it we can know the information of the caller.
Demo1:
Source:
function callerDemo() {
if (callerDemo.caller) {
var a= callerDemo.caller.toString();
alert(a);
} else {
alert("this is a top function");
}
}
callerDemo();
Result:
Demo2:
Source:
function callerDemo() {
if (callerDemo.caller) {
var a= callerDemo.caller.toString();
alert(a);
} else {
alert("this is a top function");
}
}
function handle(){
callerDemo();
}
handle();
Result: