Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Getting a reference to the currently executing function object

Reply
Thread Tools

Getting a reference to the currently executing function object

 
 
reycri@gmail.com
Guest
Posts: n/a
 
      12-04-2006
Hi,

I need to be able to do this:

var func = new Function("var me = <selfRef>; alert(me.params);");
func.params = "This is a test parameter";
window.setTimeout(func, 500);

Basically, I need to add properties to a function object and access
them within the function when it is executing. Therefore, I need to be
able to replace <selfRef> in the above example with an expression that
would resolve to the reference to the executing function object.
Note that another use I have for this is when setting the
onreadystatechange property of the XMLHttpRequest object to a function
reference.

I found one way that would work:

function whoAmI()
{
return whoAmI.caller;
}

Then, replace <selfRef> above with whoAmI(). However, this seems a bit
hacky to me and may not be the most efficient. Also, I have read that
the caller property has been deprecated and may not work in all
browsers.
I am trying to avoid using closures as they are complicated and
non-intuitive for most developers and can cause memory leaks in
browsers if not used properly
(http://www.jibbering.com/faq/faq_notes/closures.html).

Any ideas?

 
Reply With Quote
 
 
 
 
mick white
Guest
Posts: n/a
 
      12-04-2006
David Golightly wrote:

[snip]
> function Funk() { this.params = {}; }
>
> Funk.prototype.setParam = function(key, value) {
> this.params[param] = value;
> }
>


Close...

Funk.prototype.setParam = function(key, value) {
this.params[key] = value;
}

Mick

> var f = new Funk;
> f.setParam('monkeys', 23);
> f.setParam('fruit', 'grapes');
> for (var p in f.params) { alert(p + ':' + f.params[p]); }
>

 
Reply With Quote
 
 
 
 
Richard Cornford
Guest
Posts: n/a
 
      12-09-2006
David Golightly wrote:
> wrote:

<snip>
>> var func = new Function("var me = <selfRef>; alert(me.params);");
>> func.params = "This is a test parameter";
>> window.setTimeout(func, 500);

<snip>
> 1. Do not use "new Function('<function code>')" when you
> know all the details of a function before runtime.


This is not a reasonable statement as it disregards the possibility that
the desired outcome may be multiple unique function objects created in a
way that avoided the creation of closures.

> This has all the drawbacks of using eval:


Not by any means. It may have many of the drawbacks of - eval - but
using the Function constructor does not result in the direct execution
of its string argument, and also allows some control over the
environment in which the string of code used is executed (the
potentially independent provision of formal parameters for the function
and the mixing of that string with other code that may, for example,
mask out sensitive aspects of the global environment).

> it's hugely inefficient.


That very much depends on the environment. In IE6 the use of the
Function constructor can be the fastest method of creating a function
object (which is significant for people writing for an IE only context
and needing performance.

> I'm disappointed to see that the
> CLJ FAQ still has a lot of code examples using "new Function"
> when the function constructor
>
> var func = function(args) { /* code */ }
> or
> function func(args) { /* code */ }
>
> is preferred.

<snip>

Preferred by whom, and why? The code for those FAQ entries originate at
a time when inner functions were not well supported (as they were not
formally specified until ECMA 262 3rd Edition.). That makes them old but
they have not stopped working (or being less well supported) in the
meanwhile. Previous discussions on the subject have never exposed any
technical justification for changing them, just vague personal
pretences. While switching to using inner functions means forming
closures, and so in many cases having to write additional code to
mitigate IE's memory leak problem.

Blanket injunctions without justification are very dangerous things.
They tend to leave some people doing things without understanding why
they are doing them. As with most things, what should be 'preferred' is
a good technical understanding of how javascript behaves. That allows
people to make informed design decisions about what is appropriate in
their specific context.

Richard.


 
Reply With Quote
 
VK
Guest
Posts: n/a
 
      12-09-2006

wrote:
> var func = new Function("var me = <selfRef>; alert(me.params);");
> func.params = "This is a test parameter";
> window.setTimeout(func, 500);
>
> Basically, I need to add properties to a function object and access
> them within the function when it is executing. Therefore, I need to be
> able to replace <selfRef> in the above example with an expression that
> would resolve to the reference to the executing function object.
> Note that another use I have for this is when setting the
> onreadystatechange property of the XMLHttpRequest object to a function
> reference.
>
> I found one way that would work:
>
> function whoAmI()
> {
> return whoAmI.caller;
> }
>From this point I've got a bit confused of your actual: if "executing

function object" then why "caller" ? (unless a typo instead of "callee"
?)

Basically there can be two basic tasks:

1) The function is called as a method of an object instance and you
want a reference to that instance.

2) You want a reference to the executing function within the function
itself.

////////////

1) ... (ask if it was the case)

2) arguments.callee holds the needed reference. Within the "VK's
Augmentation" ) messing with strings forming the function body may
get really... messy. This way I prefer to have a conventional function
to edit and toString method overloaded to use with Function
constructor:


<script type="text/javascript">

function F(arg) {
var func = new Function(F.f);
func.params = 'Test';
window.setTimeout(func, 500);
}

/* Edit body as you used to */
F.f = function() {
window.alert(arguments.callee['params']);
}
/* Function constructor pickup start */
F.f.$tS = F.f.toString;
F.f.toString = function() {
var b = F.f.$tS();
return b.substring(b.indexOf('{')+1, b.lastIndexOf('}')-1);
}
/* Function constructor pickup end */

function init() {
F();
}

window.onload = init;
</script>


What I like is an ability to reference static member or clone it with
the same object:

function F(arg) {
// clone for individual use:
var func = new Function(F.f);

// shared static:
var func = F.f;
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
overwriting jars currently executing visionset Java 4 04-13-2007 08:33 PM
How to get the currently executing page handler? Stefan ASP .Net 2 07-26-2005 03:41 PM
Access to currently executing Page object Chris Newby ASP .Net 2 04-19-2005 09:38 PM
Having a problem using WebRequest inside code invoked by the Page_Load event to to download a page located on same server (as the currently executing page). Hasani \(remove nospam from address\) ASP .Net 9 09-29-2004 05:07 PM
How to get the name of the currently executing method? Jeff Gaynor Java 3 06-15-2004 09:34 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57