Caller method

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jaden10001@yahoo.com

    Caller method

    I have read that the function.caller method is now depricated. I see
    alot of browsers still support it but Opera 7 does not. Is there a way
    to get the name of the caller of a function in Opera 7?

  • Grant Wagner

    #2
    Re: Caller method

    <jaden10001@yah oo.com> wrote in message
    news:1103656725 .878124.140710@ f14g2000cwb.goo glegroups.com.. .[color=blue]
    > I have read that the function.caller method is now depricated. I see
    > alot of browsers still support it but Opera 7 does not. Is there a[/color]
    way[color=blue]
    > to get the name of the caller of a function in Opera 7?[/color]

    No, because a function doesn't have a single "name".

    function target() {
    }
    function a() {
    target();
    }
    b = c = d = a;
    c();
    // who called target()? -c- did, but so did -a- and -b-
    // because they all reference the same function

    Besides, I'm not sure you can obtain the "name" of the function from
    arguments.calle r anyway. What arguments.calle r was was a -reference- to
    the calling function, not it's name.

    arguments.calle r was deprecated because it is not needed, there are
    almost certainly other ways of doing what you are attempting to do.

    --
    Grant Wagner <gwagner@agrico reunited.com>
    comp.lang.javas cript FAQ - http://jibbering.com/faq


    Comment

    • Douglas Crockford

      #3
      Re: Caller method

      > I have read that the function.caller method is now depricated. I see[color=blue]
      > alot of browsers still support it but Opera 7 does not. Is there a way
      > to get the name of the caller of a function in Opera 7?[/color]

      Please tell us why you think you need a deprecated feature. Perhaps
      there is a better way to accomplish your task.


      Comment

      • codeHanger@yahoo.ca

        #4
        Re: Caller method

        Grant Wagner wrote:[color=blue]
        > <jaden10001@yah oo.com> wrote in message
        > news:1103656725 .878124.140710@ f14g2000cwb.goo glegroups.com.. .[color=green]
        > > I have read that the function.caller method is now depricated. I[/color][/color]
        see[color=blue][color=green]
        > > alot of browsers still support it but Opera 7 does not. Is there a[/color]
        > way[color=green]
        > > to get the name of the caller of a function in Opera 7?[/color]
        >
        > No, because a function doesn't have a single "name".
        >
        > function target() {
        > }
        > function a() {
        > target();
        > }
        > b = c = d = a;
        > c();
        > // who called target()? -c- did, but so did -a- and -b-
        > // because they all reference the same function
        >[/color]

        Even if there were only a single reference to the function, it wouldn't
        be possible to "reverse engineer" the name of the variable containing
        the reference. However, I don't thing that's what the OP was asking
        for.
        [color=blue]
        > Besides, I'm not sure you can obtain the "name" of the function from
        > arguments.calle r anyway. What arguments.calle r was was a -reference-[/color]
        to[color=blue]
        > the calling function, not it's name.
        >[/color]

        Correct. But a reference to a function, in particular one provided by
        arguments.calle r, can be used to obtain the string representation of
        the invoking function, which in turn will contain it's name. A robust
        RegExp then applied to the string could reliably return the name of the
        function that was used to invoke the current function (noting that i)
        arguments.calle r is null when the invocation occurs from the global
        environment, and that ii) the invoker could be nameless, i.e.,
        anonymous).
        [color=blue]
        > arguments.calle r was deprecated because it is not needed, there are
        > almost certainly other ways of doing what you are attempting to do.
        >[/color]

        Agreed. My comments fall into the quibble category -- they don't
        constitute a recommendation to the OP.

        Regards,

        ../rh

        Comment

        • jaden10001@yahoo.com

          #5
          Re: Caller method

          Well I tell you what I need to accomplish and perhaps there is a way of
          doing so.

          function a(){

          }

          function b(){
          a()
          ....
          more code
          ....
          }

          Function a needs the code which called function a or the name of the
          function. In this case function a would need to retrieve the code of
          function b or the name "b". In IE the function.caller will give me:
          function b(){
          a()
          ....
          more code
          ....
          }

          which then I can parse and retrieve all the function code or if I
          wanted the function name. Mozilla, IE, Netscape all can be used but I
          can't figure out a work around for Opera. Is there a way? Thanks

          codeHanger@yaho o.ca wrote:[color=blue]
          > Grant Wagner wrote:[color=green]
          > > <jaden10001@yah oo.com> wrote in message
          > > news:1103656725 .878124.140710@ f14g2000cwb.goo glegroups.com.. .[color=darkred]
          > > > I have read that the function.caller method is now depricated. I[/color][/color]
          > see[color=green][color=darkred]
          > > > alot of browsers still support it but Opera 7 does not. Is there[/color][/color][/color]
          a[color=blue][color=green]
          > > way[color=darkred]
          > > > to get the name of the caller of a function in Opera 7?[/color]
          > >
          > > No, because a function doesn't have a single "name".
          > >
          > > function target() {
          > > }
          > > function a() {
          > > target();
          > > }
          > > b = c = d = a;
          > > c();
          > > // who called target()? -c- did, but so did -a- and -b-
          > > // because they all reference the same function
          > >[/color]
          >
          > Even if there were only a single reference to the function, it[/color]
          wouldn't[color=blue]
          > be possible to "reverse engineer" the name of the variable containing
          > the reference. However, I don't thing that's what the OP was asking
          > for.
          >[color=green]
          > > Besides, I'm not sure you can obtain the "name" of the function[/color][/color]
          from[color=blue][color=green]
          > > arguments.calle r anyway. What arguments.calle r was was a[/color][/color]
          -reference-[color=blue]
          > to[color=green]
          > > the calling function, not it's name.
          > >[/color]
          >
          > Correct. But a reference to a function, in particular one provided by
          > arguments.calle r, can be used to obtain the string representation of
          > the invoking function, which in turn will contain it's name. A[/color]
          robust[color=blue]
          > RegExp then applied to the string could reliably return the name of[/color]
          the[color=blue]
          > function that was used to invoke the current function (noting that i)
          > arguments.calle r is null when the invocation occurs from the global
          > environment, and that ii) the invoker could be nameless, i.e.,
          > anonymous).
          >[color=green]
          > > arguments.calle r was deprecated because it is not needed, there are
          > > almost certainly other ways of doing what you are attempting to do.
          > >[/color]
          >
          > Agreed. My comments fall into the quibble category -- they don't
          > constitute a recommendation to the OP.
          >
          > Regards,
          >
          > ../rh[/color]

          Comment

          • Douglas Crockford

            #6
            Re: Caller method

            > Well I tell you what I need to accomplish and perhaps there is a way of[color=blue]
            > doing so.
            >
            > function a(){
            >
            > }
            >
            > function b(){
            > a()
            > ...
            > more code
            > ...
            > }
            >
            > Function a needs the code which called function a or the name of the
            > function. In this case function a would need to retrieve the code of
            > function b or the name "b". In IE the function.caller will give me:
            > function b(){
            > a()
            > ...
            > more code
            > ...
            > }
            >
            > which then I can parse and retrieve all the function code or if I
            > wanted the function name. Mozilla, IE, Netscape all can be used but I
            > can't figure out a work around for Opera. Is there a way? Thanks[/color]

            Why would you want to do that?

            The correct way for b to inform a is to pass the information as a
            parameter. Why do you feel you have to do this incorrectly?


            Comment

            • jaden10001@yahoo.com

              #7
              Re: Caller method


              Douglas Crockford wrote:[color=blue][color=green]
              > > Well I tell you what I need to accomplish and perhaps there is a[/color][/color]
              way of[color=blue][color=green]
              > > doing so.
              > >
              > > function a(){
              > >
              > > }
              > >
              > > function b(){
              > > a()
              > > ...
              > > more code
              > > ...
              > > }
              > >
              > > Function a needs the code which called function a or the name of[/color][/color]
              the[color=blue][color=green]
              > > function. In this case function a would need to retrieve the code[/color][/color]
              of[color=blue][color=green]
              > > function b or the name "b". In IE the function.caller will give[/color][/color]
              me:[color=blue][color=green]
              > > function b(){
              > > a()
              > > ...
              > > more code
              > > ...
              > > }
              > >
              > > which then I can parse and retrieve all the function code or if I
              > > wanted the function name. Mozilla, IE, Netscape all can be used but[/color][/color]
              I[color=blue][color=green]
              > > can't figure out a work around for Opera. Is there a way? Thanks[/color]
              >
              > Why would you want to do that?
              >
              > The correct way for b to inform a is to pass the information as a
              > parameter. Why do you feel you have to do this incorrectly?
              >
              > http://www.crockford.com/javascript/survey.html[/color]


              I am not doing anything incorrectly. Yes b could pass a itself as a
              parameter. Many browsers support the funciton.caller method so you
              would not have to, which in my instance is not favorable to do for
              other reasons which are too lengthy to get into. So please assume that
              the behavior I need is set in stone. Function b needs to know the
              function name or the function code which has called it. Opera is the
              only other browser I would like it to work in. Anybody have any ideas
              for an Opera workaround?

              Comment

              • Douglas Crockford

                #8
                Re: Caller method

                > I am not doing anything incorrectly. Yes b could pass a itself as a[color=blue]
                > parameter. Many browsers support the funciton.caller method so you
                > would not have to, which in my instance is not favorable to do for
                > other reasons which are too lengthy to get into. So please assume that
                > the behavior I need is set in stone. Function b needs to know the
                > function name or the function code which has called it. Opera is the
                > only other browser I would like it to work in. Anybody have any ideas
                > for an Opera workaround?[/color]

                funciton.caller , as you say, is not standard. You still haven't
                explained why you need to use a non-standard feature. If you did this in
                a standard way, you would not need a workaround. If your reasons are too
                lengthy to get into, then I suspect that they are not very good reasons.

                If you told us what you are trying to accomplish, we might be able to
                suggest a better way to approach it.


                Comment

                • jaden10001@yahoo.com

                  #9
                  Re: Caller method

                  Douglas,
                  As I explained in the above post I am trying to accomplish getting
                  the function name or code of the function which has called the
                  function. That is what I am trying to accomplish. If you are asking why
                  I need the function name or code I need it in order to call the super
                  classes overloaded method. I am aware that I can pass that as a
                  variable, infact it works for every browser if I pass it as an
                  argument. I would like to not have to pass that as a variable (Opera
                  currently is the only browser which I need to support that has an
                  issue). So I was wondering if there was a work around for opera to get
                  the function caller's name without passing it as an argument. If you do
                  not know a work around for this please just say so. Any body have a
                  proposed work around?

                  Douglas Crockford wrote:[color=blue][color=green]
                  > > I am not doing anything incorrectly. Yes b could pass a itself as a
                  > > parameter. Many browsers support the funciton.caller method so you
                  > > would not have to, which in my instance is not favorable to do for
                  > > other reasons which are too lengthy to get into. So please assume[/color][/color]
                  that[color=blue][color=green]
                  > > the behavior I need is set in stone. Function b needs to know the
                  > > function name or the function code which has called it. Opera is[/color][/color]
                  the[color=blue][color=green]
                  > > only other browser I would like it to work in. Anybody have any[/color][/color]
                  ideas[color=blue][color=green]
                  > > for an Opera workaround?[/color]
                  >
                  > funciton.caller , as you say, is not standard. You still haven't
                  > explained why you need to use a non-standard feature. If you did this[/color]
                  in[color=blue]
                  > a standard way, you would not need a workaround. If your reasons are[/color]
                  too[color=blue]
                  > lengthy to get into, then I suspect that they are not very good[/color]
                  reasons.[color=blue]
                  >
                  > If you told us what you are trying to accomplish, we might be able to[/color]
                  [color=blue]
                  > suggest a better way to approach it.
                  >
                  > http://www.crockford.com/#javascript[/color]

                  Comment

                  • Douglas Crockford

                    #10
                    Re: Caller method

                    > As I explained in the above post I am trying to accomplish getting[color=blue]
                    > the function name or code of the function which has called the
                    > function. That is what I am trying to accomplish. If you are asking why
                    > I need the function name or code I need it in order to call the super
                    > classes overloaded method. I am aware that I can pass that as a
                    > variable, infact it works for every browser if I pass it as an
                    > argument. I would like to not have to pass that as a variable (Opera
                    > currently is the only browser which I need to support that has an
                    > issue). So I was wondering if there was a work around for opera to get
                    > the function caller's name without passing it as an argument. If you do
                    > not know a work around for this please just say so. Any body have a
                    > proposed work around?[/color]

                    Have you looked at http://www.crockford.com/javascript/inheritance.html ?

                    Comment

                    Working...