Functions passed as arguments

  • Thread starter Richard A. DeVenezia
  • Start date
R

Richard A. DeVenezia

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

Lasse Reichstein Nielsen

Richard A. DeVenezia said:
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
 
R

Richard A. DeVenezia

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:

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top