How to get javascript object method name?

I'm currently building a bigger object and need to get faster and more specific with debugging/inspecting values/results/returns.

Now I thought about the following:

var myObject = {
    whatever: null,
    whereever: null,
    debug: false,

    someFunction: function( arg ) {
        // General - would output *all* logs for all object functions.
        // if ( true === myObject.debug )
        //  console.log( 'FunctionNameHere', arg );

        // More specific - would output *only* the log for the currently targeted function
        if ( 'FunctionName' === myObject.debug )
            console.log( 'FunctionNameHere', arg );
    },
};

This would allow me to simply define the object var debug as one function name and only log this part.

The only problem is: How would I obtain the FunctionName / someFunction ?

Sidenotes:

  • console.log( arguments.callee ); gives me the whole function source.
  • console.log( arguments.callee.name ); returns empty.
  • console.log( arguments.callee.toString() ); returns empty
  • console.log( arguments.caller ); returns undefined
  • If I take a look into the log of the whole object, I see prototype.name="Empty" and such. So no chance to get it directly out of the object.

    Thanks!


    If you want to log every function if debug is true and if debug is set to the name of function, then log only that function you don't have to hardcode this into every single function of yours.

    What you can do is dynamically rewrite the function. It is bit of a magic, but it is lot more flexible and you don't have to change anything when you add more functions or change their names.

    HERE is the working code.

    for (var key in myObject) {
      // if the keys belongs to object and it is a function
      if (myObject.hasOwnProperty(key) && (typeof myObject[key] === 'function')) {
        // overwrite this function
        myObject[key] = (function() {
          // save the previous function
          var functionName = key, functionCode = myObject[functionName];
          // return new function that will write log message and run the saved function
          return function() {
            if (myObject.debug === true || myObject.debug === functionName) {
              console.log('I am function ' + functionName, ' with arguments: ', arguments);
            }
            functionCode(arguments);
          };
        })();
      }
    }
    

    It is an anonymous function it doesn't have a name and thus you can't get to it as it is right now.

    If you had declared it like this:

    someFunction: function iNowHaveAName( arg )
    

    You would be able to get at the name in different ways depending on the browser you are in.

    In browsers that supports it, you can use arguments.callee.name . (this is fast and performance wise free)

    In browsers that doesn't you can catch an exception and find it in the stacktrace:

    try {
     i.dont.exist+=1;
    }
    catch(e) {
    //play with the stacktrace here
    }
    

    This is slow and performance wise expensive - don't do it in production code :)


    If a function does not have a name, you cannot get it. An anonymous function—which is exactly what you have—does not have a name. Assigning one or more variables or object properties to reference the function value does not give it a name.

    Consider these examples:

    var a = [function(){}];
    var b = a;
    var c = a[0];
    

    What is the "name" of that single function? a[0] ? b[0] ? c ? Why should one be chosen over the other?

    JavaScript does not have any method allowing you to ask for all reference to a particular object (including functions).

    链接地址: http://www.djcxy.com/p/95306.html

    上一篇: 为什么JavaScript中的子对象会失去全局范围?

    下一篇: 如何获取javascript对象的方法名称?