R
reycri
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?
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?