Functions passed as arguments

Discussion in 'Javascript' started by Richard A. DeVenezia, Sep 1, 2003.

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

    function sourceAsHTML() {
    var s=''
    for (var i=0; i<arguments.length; i++) {
    alert(arguments)
    s +=
    '<PRE>'
    + arguments.toString().replace (/</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.prototype.foobar = function () { /* foobar */ }

    alert (sourceAsHTML(myClass.prototype.foobar))

    .....
    the alert shows
    <PRE>function () { /* foobar */ }</PRE>
    which is close, but it doesn't show that it 'belongs' to
    myClass.prototype.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
     
    Richard A. DeVenezia, Sep 1, 2003
    #1
    1. Advertising

  2. "Richard A. DeVenezia" <> writes:

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

    ....

    > + arguments.toString().replace (/</g, '&lt;')


    You might also want to replace "&" by "&amp;".

    >
    > and it works nicely, except in the case of a class prototype assigned to an
    > anonymous function
    > i.e.
    >
    > function myClass () {}


    > function myClass.prototype.foobar = function () { /* foobar */ }


    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.

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


    You have to tell it. The function doesn't know.

    > 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) ?


    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 -
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Sep 1, 2003
    #2
    1. Advertising

  3. >
    > > 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) ?

    >
    > 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 -
    > Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
    > 'Faith without judgement merely degrades the spirit divine.'


    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
     
    Richard A. DeVenezia, Sep 1, 2003
    #3
    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. dee
    Replies:
    9
    Views:
    541
    Joseph Byrns
    Apr 15, 2005
  2. Storm
    Replies:
    1
    Views:
    477
    Chris Uppal
    May 1, 2004
  3. Martin Magnusson

    macro passed wrong number of arguments

    Martin Magnusson, Jun 15, 2004, in forum: C++
    Replies:
    4
    Views:
    564
    red floyd
    Jun 15, 2004
  4. Anand
    Replies:
    2
    Views:
    938
    Anand
    Sep 11, 2003
  5. oldyork90
    Replies:
    10
    Views:
    371
    Jorge
    Sep 27, 2008
Loading...

Share This Page