views:

99

answers:

1

Hi guys

I got following code:

function test () {

    this.testFunction = function () {
     //code to alert or return the string "testFunction"
    }

}
var testVar = new test();
testVar.testFunction();

Is there a way to find out the name of the property, which the unnamed function is assigned to? Whatever I tried yet in conjunction with "caller" and "callee" methods didn't yield any success.

Edit: The reason why I'd like to retrieve the property name is to use it for debugging messages, where I don't have to manually pass the property name to the logger. Performance would be not an issue since this is just for the developing process.

Actually the suggestion to name the function is a good idea ... I think. Does this have any obvious/well-known side effects, beside having to type in the function name twice? :-P

Additionally this brought me to the idea to add a comment at the start of a function which looks something like

/* $$NAME$$="testFunction" */

and which could also be parsed - but JavaScript comments seem to be trimmed in FireFox (unlike IE), and I rather prefer FF for developing. Would there be a way to also display/use JS comments in FF when using the "caller"/"callee" property?

+2  A: 

You can cycle through everything that's available in the instance of the object, e.g.

function test() {
    this.testFunction = function () {
     for (var i in this) {
      if (this[i] === arguments.callee) {
       alert(i); // alerts 'testFunction'
      }
     }
    }
}
var x = new test();
x.testFunction();

If your intent is to call the function recursively, you can simply name it

this.testFunction = function inner() {
    inner();
}
Alexander Gyoshev
Thank you for your really fast answers!The reason why I'd like to retrieve the property name is to use it for debugging messages, where I don't have to manually pass the property name to the logger.Actually the suggestion to name the function is a good idea ... I think. Does this have any obvious/well-known side effects? :-PAdditionally this brought me to the idea to add a comment at the start of a function which looks something like /* $$NAME$$="testFunction" */ and which could also be parsed - but JS comments seem to be trimmed in FF (unlike IE), and I rather prefer FF for developing.
ChristianS
I'm obviously new to this board. Is it common to add additional information to the original posting or is it OK to post it in a comment? Actually the formatting is a bit lost in a comment...
ChristianS
Adding additional information in edits is perfectly fine! This way readers that arrive later will know what you want to achieve from the beginning of the thread.The drawback of naming each function will be, well, that you have to name them twice. cycling through all properties of this, on the other hand, could hurdle performance a bit. If you disable it in the production environment it seems to be the better alternative.
Alexander Gyoshev
Aye, performance is not important since it's not going to stay there for a production environment but would be only used for developing.
ChristianS
nice bit of reflection. thanks for the answer
Gutzofter