By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,238 Members | 1,518 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,238 IT Pros & Developers. It's quick & easy.

Functions passed as arguments

P: n/a
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[i])
s +=
'<PRE>'
+ arguments[i].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
Jul 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
"Richard A. DeVenezia" <ra******@ix.netcom.com> writes:
I have a function source() to show the definitions of functions passed to it ....
+ arguments[i].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 - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

P: n/a
>
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 - lr*@hotpop.com
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
Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.