function name

  • 13 years ago

    I would like to get the name of the function that called the function.

    example.

    // the popup will show the source of the function that called the function popup

    function popup(str
    ){

    calledby = "\r\n" + popup.caller
    ;

    alert(calledby
    );
    }


    function cheese
    (){

    popup('Hello World!');

    }

    function onion(){
    popup('Hello World!');


    }

    onion(); // when onion() is called, the output is an alert box with

    function onion(){
    popup('Hello World!');
    }



    With the method below

    //with this method, no function name is vivible!
    popup =
    {

     msg:function(str){calledby = "\r\n" + this.msg.caller; alert(calledby
    );},

     cheese:function(){
     this.msg('Hello World!'
    );

     },

     onion:function
    (){

     this.msg('Hello World!');

     }
    }


    popup.onion(); // when called, the output doesn't show the function name or caller

    However, the following is the result of the above method

    function (){
    this.msg('Hello World!');
    }



    what I was expecting was...

    function popup.msg(){
    popup.msg('Hello World!');
    }


    or similar but it clearly isnt.
    All I need is the name of the function that made the call, eg, popup.onion(), I have used toSource, toString, valueOf methods and they either return the body of the source, the [object Object] or undefined.

    Can anyone help? or know what I need to do to get the full name of the object and function through the second method, popup.onion() ???


Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“We better hurry up and start coding, there are going to be a lot of bugs to fix.”