0

I am trying to get the name of a JavaScript instance or class method (in client-side/browser JS) while using the following OOP style for AMD modules (for usage with RequireJS):

define([...], function(...) {
  var FooBar = function() {
    //Constructor code...
  };

  FooBar.foo = function() {
    //This is a FooBar class method named "foo". How do I get its name from here?
    //...
  };

  FooBar.prototype.bar = function() {
    //This is a FooBar instance method named "bar". How do I get its name from here?
    //...
  };
});

I have seen various posts explaining how to do it in case of named functions, e.g., function foobar() {...}, but not in case of assigned anonymous functions, e.g., foobar = function() {...}.

The latter is relevant here, unless the above can be rewritten using named functions instead.

I would rather not revert to some naming-scheme convention such as foobar = function foobar_extra_name() {...}, since this involves naming a function twice. Furthermore, even if this were possible, it would still not be possible to do something like FooBar.prototype.bar = function FooBar.prototype.bar() {...} because of the dot notation, so it wouldn't be of much help.

Any thoughts about this? Any help is greatly appreciated.

Dan
  • 41
  • 3
  • 1
    What exactly is your use case? In the simplest case, you don't need to "get" the name, because you *know* that the name is `"foo"`. – Bergi Aug 25 '16 at 19:14
  • 1
    Anonymous functions have no names. What did you think would happen if the same function was assigned to two different properties? – Bergi Aug 25 '16 at 19:15
  • @Bergi: The use case is a situation in which I want to show an error message from within the function for debugging purposes without having to manually expand a stack trace in the console. Quickly printing the function name is enough. And I was simply wondering if there was some mechanism to do it (I didn't want to repeat the name in the function, since I wanted to avoid duplication of volatile information in case I would change the function name). But you're right when it comes to your second comment; I simply hoped that the way in which the function was invoked would provide some extra info. – Dan Aug 28 '16 at 21:10
  • Function names (of the public interface) rarely change, so I'd consider this duplication acceptable. What do you mean by "having to manually expand a stack trace in the console"? If you are looking for stack traces, not the particular function name only, there are ways to access this programmatically. – Bergi Aug 28 '16 at 21:43

1 Answers1

0

You can indeed rewrite your code using named functions as it follows:

var FooBar = function FooBar() {
  //Constructor code...
};

FooBar.foo = function foo() {
  //This is a FooBar class method named "foo". How do I get its name from here?
  //...
};

FooBar.prototype.bar = function bar() {
  //This is a FooBar instance method named "bar". How do I get its name from here?
  //...
};
Michele Ricciardi
  • 2,107
  • 14
  • 17
  • Also take a look at this answer to see how you can access the function name http://stackoverflow.com/a/17923727/4963159 – Michele Ricciardi Aug 25 '16 at 20:01
  • 1
    I know that this is possible, but as I said: "I would rather not revert to some naming-scheme convention such as foobar = function foobar_extra_name() {...}, since this involves naming a function twice." Furthermore, the "functionName(func)" function given in [link](http://stackoverflow.com/a/17923727/4963159) does not work when used from within a function while passing arguments.callee as its parameter (the only reference to the function that I can get from within the function itself seems to be arguments.callee, which, furthermore, is deprecated as mentioned in the link). – Dan Aug 28 '16 at 21:11