468,772 Members | 1,773 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,772 developers. It's quick & easy.

Caller method

I have read that the function.caller method is now depricated. I see
alot of browsers still support it but Opera 7 does not. Is there a way
to get the name of the caller of a function in Opera 7?

Jul 23 '05 #1
9 4093
<ja********@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
I have read that the function.caller method is now depricated. I see
alot of browsers still support it but Opera 7 does not. Is there a way to get the name of the caller of a function in Opera 7?


No, because a function doesn't have a single "name".

function target() {
}
function a() {
target();
}
b = c = d = a;
c();
// who called target()? -c- did, but so did -a- and -b-
// because they all reference the same function

Besides, I'm not sure you can obtain the "name" of the function from
arguments.caller anyway. What arguments.caller was was a -reference- to
the calling function, not it's name.

arguments.caller was deprecated because it is not needed, there are
almost certainly other ways of doing what you are attempting to do.

--
Grant Wagner <gw*****@agricoreunited.com>
comp.lang.javascript FAQ - http://jibbering.com/faq
Jul 23 '05 #2
> I have read that the function.caller method is now depricated. I see
alot of browsers still support it but Opera 7 does not. Is there a way
to get the name of the caller of a function in Opera 7?


Please tell us why you think you need a deprecated feature. Perhaps
there is a better way to accomplish your task.

http://www.crockford.com/javascript/survey.html
Jul 23 '05 #3
Grant Wagner wrote:
<ja********@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
I have read that the function.caller method is now depricated. I see alot of browsers still support it but Opera 7 does not. Is there a way
to get the name of the caller of a function in Opera 7?


No, because a function doesn't have a single "name".

function target() {
}
function a() {
target();
}
b = c = d = a;
c();
// who called target()? -c- did, but so did -a- and -b-
// because they all reference the same function


Even if there were only a single reference to the function, it wouldn't
be possible to "reverse engineer" the name of the variable containing
the reference. However, I don't thing that's what the OP was asking
for.
Besides, I'm not sure you can obtain the "name" of the function from
arguments.caller anyway. What arguments.caller was was a -reference- to the calling function, not it's name.

Correct. But a reference to a function, in particular one provided by
arguments.caller, can be used to obtain the string representation of
the invoking function, which in turn will contain it's name. A robust
RegExp then applied to the string could reliably return the name of the
function that was used to invoke the current function (noting that i)
arguments.caller is null when the invocation occurs from the global
environment, and that ii) the invoker could be nameless, i.e.,
anonymous).
arguments.caller was deprecated because it is not needed, there are
almost certainly other ways of doing what you are attempting to do.


Agreed. My comments fall into the quibble category -- they don't
constitute a recommendation to the OP.

Regards,

../rh

Jul 23 '05 #4
Well I tell you what I need to accomplish and perhaps there is a way of
doing so.

function a(){

}

function b(){
a()
....
more code
....
}

Function a needs the code which called function a or the name of the
function. In this case function a would need to retrieve the code of
function b or the name "b". In IE the function.caller will give me:
function b(){
a()
....
more code
....
}

which then I can parse and retrieve all the function code or if I
wanted the function name. Mozilla, IE, Netscape all can be used but I
can't figure out a work around for Opera. Is there a way? Thanks

co********@yahoo.ca wrote:
Grant Wagner wrote:
<ja********@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
I have read that the function.caller method is now depricated. I see alot of browsers still support it but Opera 7 does not. Is there
a way
to get the name of the caller of a function in Opera 7?
No, because a function doesn't have a single "name".

function target() {
}
function a() {
target();
}
b = c = d = a;
c();
// who called target()? -c- did, but so did -a- and -b-
// because they all reference the same function


Even if there were only a single reference to the function, it

wouldn't be possible to "reverse engineer" the name of the variable containing
the reference. However, I don't thing that's what the OP was asking
for.
Besides, I'm not sure you can obtain the "name" of the function from arguments.caller anyway. What arguments.caller was was a
-reference- to
the calling function, not it's name.

Correct. But a reference to a function, in particular one provided by
arguments.caller, can be used to obtain the string representation of
the invoking function, which in turn will contain it's name. A

robust RegExp then applied to the string could reliably return the name of the function that was used to invoke the current function (noting that i)
arguments.caller is null when the invocation occurs from the global
environment, and that ii) the invoker could be nameless, i.e.,
anonymous).
arguments.caller was deprecated because it is not needed, there are
almost certainly other ways of doing what you are attempting to do.


Agreed. My comments fall into the quibble category -- they don't
constitute a recommendation to the OP.

Regards,

../rh


Jul 23 '05 #5
> Well I tell you what I need to accomplish and perhaps there is a way of
doing so.

function a(){

}

function b(){
a()
...
more code
...
}

Function a needs the code which called function a or the name of the
function. In this case function a would need to retrieve the code of
function b or the name "b". In IE the function.caller will give me:
function b(){
a()
...
more code
...
}

which then I can parse and retrieve all the function code or if I
wanted the function name. Mozilla, IE, Netscape all can be used but I
can't figure out a work around for Opera. Is there a way? Thanks


Why would you want to do that?

The correct way for b to inform a is to pass the information as a
parameter. Why do you feel you have to do this incorrectly?

http://www.crockford.com/javascript/survey.html
Jul 23 '05 #6

Douglas Crockford wrote:
Well I tell you what I need to accomplish and perhaps there is a way of doing so.

function a(){

}

function b(){
a()
...
more code
...
}

Function a needs the code which called function a or the name of the function. In this case function a would need to retrieve the code of function b or the name "b". In IE the function.caller will give me: function b(){
a()
...
more code
...
}

which then I can parse and retrieve all the function code or if I
wanted the function name. Mozilla, IE, Netscape all can be used but I can't figure out a work around for Opera. Is there a way? Thanks


Why would you want to do that?

The correct way for b to inform a is to pass the information as a
parameter. Why do you feel you have to do this incorrectly?

http://www.crockford.com/javascript/survey.html

I am not doing anything incorrectly. Yes b could pass a itself as a
parameter. Many browsers support the funciton.caller method so you
would not have to, which in my instance is not favorable to do for
other reasons which are too lengthy to get into. So please assume that
the behavior I need is set in stone. Function b needs to know the
function name or the function code which has called it. Opera is the
only other browser I would like it to work in. Anybody have any ideas
for an Opera workaround?

Jul 23 '05 #7
> I am not doing anything incorrectly. Yes b could pass a itself as a
parameter. Many browsers support the funciton.caller method so you
would not have to, which in my instance is not favorable to do for
other reasons which are too lengthy to get into. So please assume that
the behavior I need is set in stone. Function b needs to know the
function name or the function code which has called it. Opera is the
only other browser I would like it to work in. Anybody have any ideas
for an Opera workaround?


funciton.caller, as you say, is not standard. You still haven't
explained why you need to use a non-standard feature. If you did this in
a standard way, you would not need a workaround. If your reasons are too
lengthy to get into, then I suspect that they are not very good reasons.

If you told us what you are trying to accomplish, we might be able to
suggest a better way to approach it.

http://www.crockford.com/#javascript
Jul 23 '05 #8
Douglas,
As I explained in the above post I am trying to accomplish getting
the function name or code of the function which has called the
function. That is what I am trying to accomplish. If you are asking why
I need the function name or code I need it in order to call the super
classes overloaded method. I am aware that I can pass that as a
variable, infact it works for every browser if I pass it as an
argument. I would like to not have to pass that as a variable (Opera
currently is the only browser which I need to support that has an
issue). So I was wondering if there was a work around for opera to get
the function caller's name without passing it as an argument. If you do
not know a work around for this please just say so. Any body have a
proposed work around?

Douglas Crockford wrote:
I am not doing anything incorrectly. Yes b could pass a itself as a
parameter. Many browsers support the funciton.caller method so you
would not have to, which in my instance is not favorable to do for
other reasons which are too lengthy to get into. So please assume that the behavior I need is set in stone. Function b needs to know the
function name or the function code which has called it. Opera is the only other browser I would like it to work in. Anybody have any ideas for an Opera workaround?
funciton.caller, as you say, is not standard. You still haven't
explained why you need to use a non-standard feature. If you did this

in a standard way, you would not need a workaround. If your reasons are too lengthy to get into, then I suspect that they are not very good reasons.
If you told us what you are trying to accomplish, we might be able to suggest a better way to approach it.

http://www.crockford.com/#javascript


Jul 23 '05 #9
> As I explained in the above post I am trying to accomplish getting
the function name or code of the function which has called the
function. That is what I am trying to accomplish. If you are asking why
I need the function name or code I need it in order to call the super
classes overloaded method. I am aware that I can pass that as a
variable, infact it works for every browser if I pass it as an
argument. I would like to not have to pass that as a variable (Opera
currently is the only browser which I need to support that has an
issue). So I was wondering if there was a work around for opera to get
the function caller's name without passing it as an argument. If you do
not know a work around for this please just say so. Any body have a
proposed work around?


Have you looked at http://www.crockford.com/javascript/inheritance.html ?
Jul 23 '05 #10

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Koen Van Herck | last post: by
2 posts views Thread by David Abrahams | last post: by
2 posts views Thread by Fabio Cannizzo | last post: by
14 posts views Thread by Genival | last post: by
5 posts views Thread by pamelafluente | last post: by
7 posts views Thread by =?UTF-8?B?QW50w7NuaW8gTWFycXVlcw==?= | last post: by
1 post views Thread by CARIGAR | last post: by
2 posts views Thread by Marin | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.