Hướng dẫn get function name javascript

Does anyone know if there is a way to get JavaScript function name. For example I got a function like

function test1[]{
alert[1];
}

I have it in my head section. Then I create an object obj1 and put my function there

obj1.func = test1;

When I call a method in obj1 object, do I have any way to get my function name [test1] inside of this method, except parsing the source [this.func.toString[]] of the function.

informatik01

15.8k10 gold badges73 silver badges102 bronze badges

asked Jul 5, 2010 at 10:52

1

function test[] {  alert[arguments.callee.name]; } 
b = test; 
b[];

outputs "test" [in Chrome, Firefox and probably Safari]. However, arguments.callee.name is only available from inside the function.

If you want to get name from outside you may parse it out of:

b.toString[];

but I think name property of function object might be what you need:

alert[b.name];

this however does not seem work for IE and Opera so you are left with parsing it out manually in those browsers.

The Red Pea

15.5k16 gold badges91 silver badges120 bronze badges

answered Jul 5, 2010 at 10:58

Kamil SzotKamil Szot

16.7k6 gold badges56 silver badges64 bronze badges

11

Until ES2015, there was no standard way to get the name of a function. Most current browsers support a name property on Function objects that was non-standard until ES2015, but no current version of IE does. The only option this leaves you if you need to support IE is trying to parse the name from the function's string representation, which is not a good idea. There's a [long] discussion here about it: //groups.google.com/group/comp.lang.javascript/browse_frm/thread/b85dfb2f2006c9f0

answered Jul 5, 2010 at 11:38

Tim DownTim Down

309k72 gold badges445 silver badges521 bronze badges

9

The best thing to do is:

function functionName[fun] {
  var ret = fun.toString[];
  ret = ret.substr['function '.length];
  ret = ret.substr[0, ret.indexOf['[']];
  return ret;
}

Note: Using Function.caller is non-standard and arguments.callee is forbidden in strict mode.

answered Feb 19, 2015 at 11:58

Vlad A. IonescuVlad A. Ionescu

2,5201 gold badge15 silver badges19 bronze badges

1

Here's what I use to put class names in error messages. It includes code to get the name of functions, which works in most browsers.

Obviously, there is no standard way that always works, so you should always provide a name that can be used if no other name is found.

var nameFromToStringRegex = /^function\s?[[^\s[]*]/;

/**
 * Gets the classname of an object or function if it can.  Otherwise returns the provided default.
 *
 * Getting the name of a function is not a standard feature, so while this will work in many
 * cases, it should not be relied upon except for informational messages [e.g. logging and Error
 * messages].
 *
 * @private
 */
function className[object, defaultName] {
    var result = "";
    if [typeof object === 'function'] {
        result = object.name || object.toString[].match[nameFromToStringRegex][1];
    } else if [typeof object.constructor === 'function'] {
        result = className[object.constructor, defaultName];
    }
    return result || defaultName;
}

answered Sep 23, 2013 at 21:36

kybernetikoskybernetikos

8,0411 gold badge45 silver badges54 bronze badges

2

This is probably the best way to do it:

var myfunc = function [] {};
var funcName = myfunc.constructor.name;

This can be done outside the execution of the function, and you can check within the context of the browser console.

Happy coding!

answered Nov 20, 2014 at 16:29

1

One interesting way I'm experimenting with is a declaration like the following:

var test1 = function test1[]{
    alert[1];
};

It's a little hacky, but what ends up happening is test1 is a local variable that holds a [Function: test1] object.

Here's what happens when you use code based on it:

test1[]; //runs the function as expected
console.log[test1.name]; //prints 'test1'

So if you do the following:

obj1.func = test1;

You'll then be able to reference obj1.func.name and it'll return 'test1'.

answered Oct 7, 2012 at 2:13

jdotjdotjdotjdot

15.4k12 gold badges63 silver badges115 bronze badges

You could convert your function into a string [fn + ''] and split it later at whitespace and open bracket /\s|\[/.

var getFunctionName = function [fn] {
   return [fn + ''].split[/\s|\[/][1];
};

answered Jun 14, 2014 at 1:12

yckartyckart

30.8k9 gold badges117 silver badges127 bronze badges

1

Not the answer you're looking for? Browse other questions tagged javascript or ask your own question.

Chủ Đề