Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript, get the name of a bound function

Tags:

javascript

I've created a function and bound the arguments as below.

function myFunc(){}

let boundFunc = myFunc.bind(argument);

But then I pass this bound function as an argument to another function, where I need to get the name. The following:

function doTheThing(callable){
    console.log(callable.name + " did the thing");
}

doTheThing(boundFunc);

Prints out bound did the thing rather than myFunc did the thing. Is there any way to get the name of the bound function?


callable.caller results in Uncaught TypeError: 'caller' and 'arguments' are restricted function properties and cannot be accessed in this context. and is not browser standard.

like image 778
timlyo Avatar asked Oct 12 '25 14:10

timlyo


1 Answers

Google Chrome v 51.0.2704.103 gives a different result:

function myFunc(){}

let boundFunc = myFunc.bind(null);

function doTheThing(callable){
  console.log(callable.name + " did the thing");
}

doTheThing(boundFunc);

It prints bound myFunc did the thing, so you could get the original name with callable.name.substring(6)

like image 75
Schlaus Avatar answered Oct 14 '25 07:10

Schlaus