Function.caller if not called from within a function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Thomas Mlynarczyk

    #1

    Function.caller if not called from within a function

    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



  • Martin Honnen

    #2
    Re: Function.caller if not called from within a function



    Thomas Mlynarczyk wrote:

    [color=blue]
    > 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?[/color]

    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/

    Comment

    • Thomas Mlynarczyk

      #3
      Re: Function.caller if not called from within a function

      Also sprach Martin Honnen:
      [color=blue]
      > What kind of access do you try?[/color]

      Basically the same way as in your example code.
      [color=blue]
      > Of course if there is no caller that property is null so you can't do much[/color]
      with it.

      But if I call "caller" and it doesn't exist, I should get an error message,
      which does not happen.
      [color=blue]
      > The following test case works for me without problems in Firefox 1.0
      > and in some recent Mozilla nightly:[/color]

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



      Comment

      • Martin Honnen

        #4
        Re: Function.caller if not called from within a function



        Thomas Mlynarczyk wrote:

        [color=blue]
        > 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.[/color]

        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/

        Comment

        • Thomas Mlynarczyk

          #5
          Re: Function.caller if not called from within a function

          Also sprach Martin Honnen:
          [color=blue]
          > 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.[/color]

          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?


          Comment

          Working...