Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > get object name from within object

Reply
Thread Tools

get object name from within object

 
 
warteschlange
Guest
Posts: n/a
 
      07-22-2005
is there a way to find out the object/function name from inside
object/function.

function coffee(){
alert(this.someHowGetMyNameFuncOrVar); => should give me 'coffee'
}

var milk = new coffee(); => should give me 'milk'

 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      07-22-2005


warteschlange wrote:

> is there a way to find out the object/function name from inside
> object/function.


Inside of a function you can access
arguments.callee
which is the function called. With Mozilla JavaScript functions have a
name property so there you can access
arguments.callee.name
to get the function name but other browsers/implementations do not
implement that name property so there if you need the function name you
would need to try to parse it out of the function's source code
representation, i.e. you need to parse
arguments.callee.toString()
for the function name.


--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
 
 
 
Baconbutty
Guest
Posts: n/a
 
      07-22-2005
I guess that would be a problem for anonymous functions.

var myFunction=function(){};

 
Reply With Quote
 
Baconbutty
Guest
Posts: n/a
 
      07-22-2005
Sorry, that was a statement of the obvious.

 
Reply With Quote
 
Matt Kruse
Guest
Posts: n/a
 
      07-22-2005
warteschlange wrote:
> is there a way to find out the object/function name from inside
> object/function.
> function coffee(){
> alert(this.someHowGetMyNameFuncOrVar); => should give me 'coffee'
> }
> var milk = new coffee(); => should give me 'milk'


A good question is, "why?"

Perhaps there is a better way to do what you're trying to do without needing
this functionality.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com


 
Reply With Quote
 
Lee
Guest
Posts: n/a
 
      07-22-2005
warteschlange said:
>
>is there a way to find out the object/function name from inside
>object/function.
>
>function coffee(){
> alert(this.someHowGetMyNameFuncOrVar); => should give me 'coffee'
>}
>
>var milk = new coffee(); => should give me 'milk'


In your last line, the variable "milk" may not even exist at
the time that the function is executing.

The Object that is created is completely distinct from any
variable that contains a reference to it. The Object doesn't
have any intrinsic name. Any number of named variables may
hold references to the Object.

 
Reply With Quote
 
VK
Guest
Posts: n/a
 
      07-23-2005


warteschlange wrote:
> is there a way to find out the object/function name from inside
> object/function.
>
> function coffee(){
> alert(this.someHowGetMyNameFuncOrVar); => should give me 'coffee'
> }
>
> var milk = new coffee(); => should give me 'milk'


Quick'N'Durty (RegExp, constructor chain and so are welcome):

<script type="text/javascript">

function FunctionOne() {
FunctionTwo();
}

function FunctionTwo() {
var s = '';
if (arguments.callee) {
var thisName = getObjectName(arguments.callee);
s+= "My name is " + thisName;
}
if (arguments.caller) {
s+= "\nI'm called by ";
s+= getObjectName(eval(thisName+".caller"));
}
alert(s);
}

function getObjectName(obj) {
var tmp = obj.toString();
return tmp.substring(tmp.indexOf(' ')+1,tmp.indexOf('('));
}
</script>

 
Reply With Quote
 
Christopher J. Hahn
Guest
Posts: n/a
 
      07-23-2005
VK wrote:
> warteschlange wrote:
> > is there a way to find out the object/function name from inside
> > object/function.
> >
> > function coffee(){
> > alert(this.someHowGetMyNameFuncOrVar); => should give me 'coffee'
> > }
> >
> > var milk = new coffee(); => should give me 'milk'

>
> Quick'N'Durty (RegExp, constructor chain and so are welcome):
>
> <script type="text/javascript">
>
> function FunctionOne() {
> FunctionTwo();
> }
>
> function FunctionTwo() {
> var s = '';
> if (arguments.callee) {
> var thisName = getObjectName(arguments.callee);
> s+= "My name is " + thisName;
> }
> if (arguments.caller) {
> s+= "\nI'm called by ";
> s+= getObjectName(eval(thisName+".caller"));
> }
> alert(s);
> }
>
> function getObjectName(obj) {
> var tmp = obj.toString();
> return tmp.substring(tmp.indexOf(' ')+1,tmp.indexOf('('));
> }
> </script>


You're trying to get the object's constructor's name (coffee, in this
case), (and doing it badly, besides), but I gather what the OP wants is
the name of the variable containing the reference to the object (milk,
in this case).

Part of the problem is I'm pretty sure is that milk doesn't yet exist
at the time that coffee is called.

In 'milk = new coffee()', I'm *fairly* sure that coffee() is called
first, then = works its magic to autovivify it if necessary, et cetera,
then actually does the assignment.

All of that is better explained (or debunked entirely) by a real expert
on the language.

One way to workaround might be to say
milk = new coffee( 'milk' );
but that's not really what the OP wants.


The other problem is here:
milk = new coffee( 'milk' );
sugar = milk;

sugar and milk now reference to the same object, but the object thinks
it's milk. It doesn't know about sugar.

milk = new coffee( 'milk' );
sugar = milk;
milk = new coffee( 'milk' );

and now two objects think they are milk, when one of them is really
sugar. sugar thinks it's milk, and if it tries to operate on what it
thinks itself really is, it'll actually be operating on milk.

I don't believe that it's possible (or possibly adviseable) to do what
the OP is after, but maybe if we understood why he wanted to do it, we
could suggest another way to achieve the ultimate end goal.

 
Reply With Quote
 
warteschlange
Guest
Posts: n/a
 
      07-23-2005
Matt Kruse wrote:
> A good question is, "why?"


Christopher J. Hahn wrote:
> but maybe if we understood why he wanted to do it, we
> could suggest another way to achieve the ultimate end goal.



ok,
I will explain the reason '''why''' with an example of a function,
(leaving beside php-classes/ js-object)

what i do is a js2php interface
so i can use all my php functions directly from javascript.
this works perfectly so far.

var jsVar = trulyPHPFunc( args);
The point is, that i have to hardcode for each function its name as
parameter
because i create all javascript functions dynamically as a 'mirror' of
my phpfunctions/classes
and don't know the name during creation.

for example:

i have a php function/class
PHP Code:
function trulyPHPFuncarguments ){
return 
'someStuff'

i create dynamic a jsfunction
[JS]
function trulyPHPFunc( arguments ){
return call_HTTP_Request( {func:callee , args:arguments } );
// where calle is the name
}
[/JS]

don't remind me:
* this works only 'sync' and it can block the browser
* i know AJAX/XML_RPC/SOAP
* and no, there is no security leak

In a intranet it is like heaven to write apps.
no longer need to 'submit' the classic way,
just onchange='doPHPfunc(this)'

==use the best of both worlds==

 
Reply With Quote
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      07-23-2005
"VK" <> writes:

> warteschlange wrote:
>> is there a way to find out the object/function name from inside
>> object/function.


>> function coffee(){
>> alert(this.someHowGetMyNameFuncOrVar); => should give me 'coffee'
>> }


Possible, but why bother when you can just use the string "coffee",
because the code to find the name will be inside the function with the
declared name "coffee" anyway. Ofcourse, just using a string violates
the DRY principle (Don't Repeat Yourself), but the alternatives are
not as well supported.

>>
>> var milk = new coffee(); => should give me 'milk'


Impossible.

....
> var thisName = getObjectName(arguments.callee);


Here you find the name of arguments.callee ...

> if (arguments.caller) {
> s+= "\nI'm called by ";
> s+= getObjectName(eval(thisName+".caller"));


and here you look that name up. As usual, the use of eval is
not necessary, just use the value directly:
s+= getObjectName(arguments.callee.caller);

However, since you guard this with
if (arguments.caller) {
you might mean to do just
s+= getObjectName(arguments.caller);

In any case, arguments.caller and func.caller are not part of the
ECMAScript standard, and is not implemented in all browsers.

> function getObjectName(obj) {
> var tmp = obj.toString();
> return tmp.substring(tmp.indexOf(' ')+1,tmp.indexOf('('));


This can error if the obj is a function from a function expression,
see, e.g.,
alert((function(){alert("goo");}).toString())


Still, arguments.callee is the only way to access the current function
without knowing its name. It's just that it only works inside that function,
and at that point, you should know the name.
/L
--
Lasse Reichstein Nielsen -
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
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
adding a variable name to a hash to name is part of the variable name Bobby Chamness Perl 2 04-22-2007 09:54 PM
print("my name is {name}, and {age}-year old {gender}", name, age, gender); =?iso-8859-1?B?bW9vcJk=?= Java 7 01-02-2006 04:39 PM
Getting name of object instance from within object method Martin Javascript 6 08-20-2004 12:24 PM
name = name.substring(0, name.lastIndexOf('.')); Help please Jack-2 Javascript 3 12-24-2003 04:39 PM
Re: Urgent! how to get object name, method name and attribute name based on the strings? ding feng C++ 2 06-25-2003 01:18 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