I want to do the opposite of Get JavaScript function-object from its name as a string?
That is, given:
function foo()
{}
function bar(callback)
{
You can extract the object and function name with:
function getFunctionName()
{
return (new Error()).stack.split('\n')[2].split(' ')[5];
}
For example:
function MyObject()
{
}
MyObject.prototype.hi = function hi()
{
console.log(getFunctionName());
};
var myObject = new MyObject();
myObject.hi(); // outputs "MyObject.hi"