Finding a Function's Name

© 2011, Martin Rinehart

Suppose you wanted to pass a function as an argument to a processing method. Suppose you also had use for the name of the function within the processing method. Given that your function reference is named func you can access its name using in standards-compliant browsers. Good to know, but what about MSIE?

Try this:

    function meth( func ) {
        if ( ) { alert( 'name ' + ); }
        else {
            func.toString().match(  /.*function\s+(\w+)/  );
            alert( 'regex ' +  RegExp.$1 );

Let me ask a question: for whose benefit is Microsoft run? Before you give a quick answer, take a look at the stock price (ticker MSFT) over the last decade.

Feedback: MartinRinehart at gmail dot com

# # #