0
function myname()
{
Console.log(arguments.callee.toString().match(/function ([^\(]+)/)[1]);
}

this print myname in console. but I have to print this

myname: function() {
Console.log(arguments.callee.toString().match(/function ([^\(]+)/)[1]);
}

which is throwing an error. Is there a way to print myname from problem defined above. I have to call it from inside only.

0

2 Answers 2

2

Are you sure you meant to use ":", and not "="

var myname = function myname() {
console.log(arguments.callee.toString().match(/function ([^\(]+)/)[1]);
}

this works.

or did you mean this?

var myObj = {
  myname: function myname() {
    console.log(arguments.callee.toString().match(/function ([^\(]+)/)[1]);
  }
}

myObj.myname()

or you can try this, quick n' dirty. You can make it more robust: There are limitations to this usage.

function findKey(val) {
   for (v in myObj) {
     if( myObj[v].toString() === val) {
       return v
     }
   }
}

var myObj = {
  myname: function() {
    console.log(findKey(arguments.callee.toString()));
  }
}

myObj.myname()
Sign up to request clarification or add additional context in comments.

2 Comments

var myObj = { myname: function () { console.log(arguments.callee.toString().match(/function ([^(]+)/)[1]); } } myObj.myname();
I modified my answer with a workaround, BUT - caveat: it works of a different premise. So, it might not be what you need.
2
myname: function() {
  console.log(arguments.callee.toString().match(/function ([^\(]+)/)[1]);
}

In above code, myname is not the name of the funciton, it is the name of property that points to an anonymous function.

If you want the name of the function, you will have to give one. So, for example

var a = {
   myname:function myname() 
     {  
       console.log(arguments.callee.toString().match(/function ([^\(]+)/)[1]);
     }
};
a.myname();

this will print myname.

For some more information read this. - https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments/callee

Comments

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.