1

Is it possible to reference this function's name? For example:

'use strict'
function myClass(){}

myClass.prototype.myName = function myName(data,callback){
    console.log("This function has been called: " + "{???}"); // "myName"
}
myClass.prototype.myAge = function(data,callback){
    console.log("This function has been called: " + "{???}"); // "myAge"
}
var a = new myClass();
a.myName(); // 'myName'
a.myAge();  // 'myAge';

How can I reference "myName" in strict mode like that?

9
  • 2
    Are you asking about the myName property or the named function? You can clarify by not making the function name and the property names the same Commented Jan 26, 2015 at 23:55
  • Any way to dynamically pull the name of the method being executed, so i wouldn't have to hard-code the function name within each console.log. Commented Jan 26, 2015 at 23:58
  • @d-_-b you can instantiate an Error instance and get a stack trace. Commented Jan 27, 2015 at 0:00
  • 1
    Neither of them actually. Why do you need the name? Commented Jan 27, 2015 at 0:05
  • 1
    @d-_-b Neither is possible, you could have two properties pointing to the same function. stackoverflow.com/questions/4260308/… Also, in strict mode, you do not have access to a function's reference so you cannot know its name even if you gave it a name Commented Jan 27, 2015 at 0:07

2 Answers 2

1

If this is just for debugging, you can use the following, which uses the stack trace to find a function's name, taking advantage of the browser magic that it does to find the "names" of functions. Tested in FF, Chrome and IE 10.

function MyClass() {};

MyClass.prototype.myName = function() {
  console.log(getCallerName(), 'I am here');
};

MyClass.prototype.myOtherName = function() {
  console.log(getCallerName(), 'I am here again');
};


function doMe() {
  console.log(getCallerName(), 'I am here in a named function');
};


function getCallerName() {
  try {
    throw new Error();
  } catch (e) {
    if (e.stack) {
      var lines = e.stack.split('\n');
      // FF (Maybe, Opera and Safari)
      var ffMatch = /\b([a-zA-Z1-9\$_\.]*)@/.exec(lines[1]);
      if (ffMatch) {
        return ffMatch[1];
      }

      // IE 10+ and chrome
      var chromeMatch = /at (.*) /.exec(lines[2]);
      if (chromeMatch) {
        return chromeMatch[1];
      }

    }
    return 'unknown function';
  }
}

var a = new MyClass();

a.myName();
a.myOtherName();
doMe();

Play with it at http://jsfiddle.net/ob0w4z3k/5/

Sign up to request clarification or add additional context in comments.

Comments

0

You can use myName.name:

(function myName(){
    console.log("This functions name is " + myName.name);
})(); // "This functions name is myName"

However, note that this was introduced in ECMAScript 6, so most browsers don't support it yet.

Only Firefox has supported it for a long time, because it was a non-standard feature, before ES6 spec was made.

Also see the MDN article.

5 Comments

But this requires knowing the name of the function? If you know the name, why would you use its .name?
@JuanMendes No, it only requires a reference to the function.
But you are not using a reference to the function, I think the OP would like something that gives you that reference without knowing the name of the function, such as arguments.callee but you can't use that because that doesn't work in strict mode.
@Oriol I think he means that if you know to type myName.name you can just type "myName".
@Pointy Yes, true, if you know the function i called myName. But in case you don't know that (e.g if you assign the function to another variable or pass it to another function), you can use .name. I think it's not much assuming that, if the OP wants to know the name of a function, he has a reference to the function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.