arguments.caller Property | Flash 6 |
a reference to the calling function | read/write |
The caller property stores a reference to the function that called the current function. If the current function was not called by another function, caller is null. For example:
// Create a function that calls two other functions function formatPage () { formatTitle(); formatBody(); } function formatTitle () { // Check if the function was called by formatPage( ) trace(arguments.caller = = formatPage); } function formatBody () { // Check if the function was called by formatPage( ) trace(arguments.caller = = formatPage); } // Now call formatTitle( ) and formatBody( ) from formatPage( ) formatPage(); // Displays: true and true // Then call formatTitle( ) directly formatTitle(); // arguments.caller will be null. Displays: false
We can use caller to execute or identify a calling function and to check whether a function was called directly or by another function.
Note that in Netscape 4, JavaScript's arguments.caller refers to the Arguments object of the calling function, not the calling function itself. In JavaScript, arguments.caller is deprecated as of version 1.3, and it is not supported by Internet Explorer or later versions of Netscape.
Macromedia's initial documentation for Flash MX incorrectly claims that arguments.caller refers to the calling function's Arguments object. It refers, in fact, to the calling function itself, not the caller's Arguments object.
The following code shows a function that aborts itself when called directly, ensuring that it is only called by a larger routine:
function subFunction () { if (arguments.caller = = null) { return "Function cannot be called directly."; } // proceed with function... }