Functions passed as arguments

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Richard A. DeVenezia

    Functions passed as arguments

    I have a function source() to show the definitions of functions passed to it

    function sourceAsHTML() {
    var s=''
    for (var i=0; i<arguments.len gth; i++) {
    alert(arguments[i])
    s +=
    '<PRE>'
    + arguments[i].toString().rep lace (/</g, '&lt;')
    + '</PRE>'
    }
    return s
    }

    and it works nicely, except in the case of a class prototype assigned to an
    anonymous function
    i.e.

    function myClass () {}
    function myClass.prototy pe.foobar = function () { /* foobar */ }

    alert (sourceAsHTML(m yClass.prototyp e.foobar))

    .....
    the alert shows
    <PRE>function () { /* foobar */ }</PRE>
    which is close, but it doesn't show that it 'belongs' to
    myClass.prototy pe.foobar.


    So the question is, when passed a function as an arg is there a way to know
    the function name or context (i.e. prototype) ?
    --
    Richard A. DeVenezia


  • Lasse Reichstein Nielsen

    #2
    Re: Functions passed as arguments

    "Richard A. DeVenezia" <radevenz@ix.ne tcom.com> writes:
    [color=blue]
    > I have a function source() to show the definitions of functions passed to it[/color]
    ....
    [color=blue]
    > + arguments[i].toString().rep lace (/</g, '&lt;')[/color]

    You might also want to replace "&" by "&amp;".
    [color=blue]
    >
    > and it works nicely, except in the case of a class prototype assigned to an
    > anonymous function
    > i.e.
    >
    > function myClass () {}[/color]
    [color=blue]
    > function myClass.prototy pe.foobar = function () { /* foobar */ }[/color]

    The first "function" is wrong.
    What you have is an anonymous function. You then assign it to some
    arbitrary object property. The function itself couldn't care less,
    so there is no way to get that information from the function object
    alone.
    [color=blue]
    > alert (sourceAsHTML(m yClass.prototyp e.foobar))
    > ....
    > the alert shows
    > <PRE>function () { /* foobar */ }</PRE>
    > which is close, but it doesn't show that it 'belongs' to
    > myClass.prototy pe.foobar.[/color]

    You have to tell it. The function doesn't know.
    [color=blue]
    > So the question is, when passed a function as an arg is there a way to know
    > the function name or context (i.e. prototype) ?[/color]

    The function you created doesn't have a name. It can have more than one
    context, but the function don't have to know until it is called.

    So, no.

    /L
    --
    Lasse Reichstein Nielsen - lrn@hotpop.com
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
    'Faith without judgement merely degrades the spirit divine.'

    Comment

    • Richard A. DeVenezia

      #3
      Re: Functions passed as arguments

      >[color=blue][color=green]
      > > So the question is, when passed a function as an arg is there a way to[/color][/color]
      know[color=blue][color=green]
      > > the function name or context (i.e. prototype) ?[/color]
      >
      > The function you created doesn't have a name. It can have more than one
      > context, but the function don't have to know until it is called.
      >
      > So, no.
      >
      > /L
      > --
      > Lasse Reichstein Nielsen - lrn@hotpop.com
      > Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
      > 'Faith without judgement merely degrades the spirit divine.'[/color]

      Lasse:

      Thanks, I thought as much, but wanted to be sure.
      So for anonymous functions I pass the function name as a string.
      In my loop if the arg is a string the 'source' I make HTMLy is the arg
      window[arg]

      Richard


      Comment

      Working...