timlyo
timlyo

Reputation: 2384

Javascript, get the name of a bound function

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.

Upvotes: 4

Views: 2743

Answers (4)

brat
brat

Reputation: 584

You can compare a bound function to see if its same as the original "unbound" function. Although it's a bit of a round about way of doing it. You first make an instance of the bound function using new and then make the comparison with that instance constructor and compare it with the "original" function:

instance.constructor === originalFunction

Here is some example code:

function funcA(aNumber) {
    let result = aNumber * 2;
    return result;
}

function funcB(aNumber)  {
    let result = aNumber * 3;
    return result;
}

// binding above functions with different arguments
let boundFuncA = funcA.bind(null, 2);
let boundFuncB = funcB.bind(null, 3);
let secondBoundFuncB = funcB.bind(null, 4);

// making instances of bound functions for comparison with "originals".
let instanceOfBoundFuncA = new boundFuncA();
let instanceOfBoundFuncB = new boundFuncB();
let instanceOfSecondBoundFuncB = new secondBoundFuncB();
let instanceOfFuncB = new funcB();

//comparing in different ways.
console.log(instanceOfBoundFuncA.constructor === funcA);  // true
console.log(instanceOfBoundFuncA.constructor === funcB); // false
console.log(instanceOfBoundFuncB.constructor === funcB);// true
console.log(instanceOfFuncB.constructor === funcB);        // true
console.log(instanceOfBoundFuncB.constructor === instanceOfSecondBoundFuncB.constructor); // true

Hereby you can check if the bound function is same as the "root" function, even if it has arguments bound to it. And even two bound function with each other as long as they have same "root" function so to speak.

Original Author and solution can be found here: Original

Upvotes: -1

mara-mfa
mara-mfa

Reputation: 895

If you're using node (tested now on v9.2.0), try

import util from 'util'

function myFunc(){}
let boundFunc = myFunc.bind(null)
let functionInfo = util.format(boundFunc)

console.log(functionInfo) // [Function: bound myFunc]

.. and extract it from there

Upvotes: 0

timlyo
timlyo

Reputation: 2384

Long story short callable.name does work, and produces bound myFunc.


My version wasn't working because I was using transpiled typescript. This snippet:

class MyClass{
  static doTheThing(callable) {}
}

let myFunc = MyClass.doTheThing.bind(null);

function handleCall(callable){
  console.log(callable.name)
}

handleCall(myFunc);

produces:

var MyClass = (function () {
    function MyClass() {
    }
    MyClass.doTheThing = function (callable) {};
    return MyClass;
}());
var myFunc = MyClass.doTheThing.bind(null);
function handleCall(callable) {
    console.log(callable.name);
}
handleCall(myFunc);

The key is the line MyClass.doTheThing = function (callable) {}; This makes MyClass.doTheThing an anonymous function therefore returning undefined for it's name. This results in callable.name returning "bound " + undefined or "bound ".

So in short, you can get the name of a bound function, but a girl function does not have a name.

Upvotes: 0

Schlaus
Schlaus

Reputation: 19212

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)

Upvotes: 4

Related Questions