arguments.caller Property

ActionScript for Flash MX: The Definitive Guide, 2nd Edition
By Colin Moock
Chapter 18.  ActionScript Language Reference
arguments.caller Property Flash 6

a reference to the calling function read/write
arguments.caller

Description

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.

Usage

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.

Bugs

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.

Example

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... }

See Also

arguments.callee

     



    ActionScript for Flash MX. The Definitive Guide
    ActionScript for Flash MX: The Definitive Guide, Second Edition
    ISBN: 059600396X
    EAN: 2147483647
    Year: 2002
    Pages: 780
    Authors: Colin Moock

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net