Function.caller if not called from within a function

Discussion in 'Javascript' started by Thomas Mlynarczyk, Feb 26, 2005.

  1. Hi,

    I stumbled over a strange behaviour of Mozilla. When I want to access the
    caller property of a function that was not called from within another
    function, Mozilla seems to abort the script. No error message, no hang, just
    stopping script execution at that point. Why? And what is the remedy?

    Greetings,
    Thomas
    Thomas Mlynarczyk, Feb 26, 2005
    #1
    1. Advertising

  2. Thomas Mlynarczyk wrote:


    > I stumbled over a strange behaviour of Mozilla. When I want to access the
    > caller property of a function that was not called from within another
    > function, Mozilla seems to abort the script. No error message, no hang, just
    > stopping script execution at that point. Why? And what is the remedy?


    What kind of access do you try? Of course if there is no caller that
    property is null so you can't do much with it.

    The following test case works for me without problems in Firefox 1.0 and
    in some recent Mozilla nightly:

    <html lang="en">
    <head>
    <title>caller property of function</title>
    <script type="text/javascript">
    function f () {
    var result = 'function f called : ';
    result += 'typeof f.caller: ' + (typeof f.caller) + '; ';
    result += 'f.caller: ' + f.caller;
    document.write('<p>' + result + '<\/p>\r\n');
    }
    function g () {
    f();
    }
    </script>
    </head>
    <body>

    <h1>caller property of function</h1>

    <script type="text/javascript">
    f();
    </script>

    <script type="text/javascript">
    g();
    </script>

    </body>
    </html>

    Output is

    function f called : typeof f.caller: object; f.caller: null

    function f called : typeof f.caller: function; f.caller: function g() {
    f(); }

    and I see nothing wrong with that, there is certainly no hang.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Feb 26, 2005
    #2
    1. Advertising

  3. Also sprach Martin Honnen:

    > What kind of access do you try?


    Basically the same way as in your example code.

    > Of course if there is no caller that property is null so you can't do much

    with it.

    But if I call "caller" and it doesn't exist, I should get an error message,
    which does not happen.

    > The following test case works for me without problems in Firefox 1.0
    > and in some recent Mozilla nightly:


    Code:
    [color=blue]
    > Output is
    > function f called : typeof f.caller: object; f.caller: null
    > function f called : typeof f.caller: function; f.caller: function g() {[/color]
    f(); }[color=blue]
    > and I see nothing wrong with that, there is certainly no hang.[/color]
    
    Mozilla 1.1a (Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.1a)
    Gecko/20020611) only shows the second line of the output.
    Netscape 6 (Mozilla/5.0 (Windows; U; Win98; de-DE; PC-Welt-DE)
    Gecko/20001106 Netscape6/6.0) says typeof f.caller is undefined (for both
    calls).
    
    OK, these are not the most recent versions of both browers and I can cope
    with the "undefined" case, but that Mozilla 1.1a thing is annoying - there
    is no error message and it seems the whole function call is aborted. Even
    when I comment out the line which actually accesses f.caller, the result is
    the same - merely asking for the type of f.caller causes the problem.
    Thomas Mlynarczyk, Feb 26, 2005
    #3
  4. Thomas Mlynarczyk wrote:


    > Mozilla 1.1a (Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.1a)
    > Gecko/20020611) only shows the second line of the output.
    > Netscape 6 (Mozilla/5.0 (Windows; U; Win98; de-DE; PC-Welt-DE)
    > Gecko/20001106 Netscape6/6.0) says typeof f.caller is undefined (for both
    > calls).
    >
    > OK, these are not the most recent versions of both browers and I can cope
    > with the "undefined" case, but that Mozilla 1.1a thing is annoying - there
    > is no error message and it seems the whole function call is aborted. Even
    > when I comment out the line which actually accesses f.caller, the result is
    > the same - merely asking for the type of f.caller causes the problem.


    As for Netscape 6, yes, bug
    https://bugzilla.mozilla.org/show_bug.cgi?id=65683 suggests that at that
    time function.caller was not implemented at all which is fine I think,
    it is not required by the ECMAScript standard.
    I can understand that the Mozilla 1.1a behavior you find hurts your
    script attempts but obviously that problem has been fixed in later
    releases so there is not much to do about that now. I think bug
    https://bugzilla.mozilla.org/show_bug.cgi?id=168081 covers the problem
    and got somehow fixed by other changes without anyone trying to fix it.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Feb 26, 2005
    #4
  5. Also sprach Martin Honnen:

    > As for Netscape 6, yes, bug
    > https://bugzilla.mozilla.org/show_bug.cgi?id=65683 suggests that at
    > that time function.caller was not implemented at all which is fine I
    > think, it is not required by the ECMAScript standard.
    > I can understand that the Mozilla 1.1a behavior you find hurts your
    > script attempts but obviously that problem has been fixed in later
    > releases so there is not much to do about that now. I think bug
    > https://bugzilla.mozilla.org/show_bug.cgi?id=168081 covers the problem
    > and got somehow fixed by other changes without anyone trying to fix
    > it.


    So there's no way for Mozilla 1.1a to make it at least behave as if it did
    not support Function.caller if the function wasn't called from within
    another function?
    Thomas Mlynarczyk, Feb 26, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SG1udA==?=
    Replies:
    5
    Views:
    2,648
    IcingDeath
    Jun 25, 2006
  2. Apricot
    Replies:
    4
    Views:
    524
    velthuijsen
    Apr 16, 2004
  3. Peter Moscatt
    Replies:
    4
    Views:
    327
    Peter Moscatt
    Apr 18, 2005
  4. er
    Replies:
    2
    Views:
    331
  5. Mark
    Replies:
    2
    Views:
    391
Loading...

Share This Page