4

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.

asked Jun 29, 2016 at 8:59
1
  • As far as I know, No. .bind() returns an entirely new function. So unless you hold an instance of the old function somewhere, you can't determine its name. Commented Jun 29, 2016 at 9:03

4 Answers 4

4

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)

answered Jun 29, 2016 at 9:06
Sign up to request clarification or add additional context in comments.

4 Comments

Also works in FF 49.0a2. Might I suggest /^bound (.*)$/.exec(callable.name)[1] for the original name though, as that would not fail silently if the name for some reason did not match the pattern "bound ..."?
I'm finding that snippet is working for me as well, but the original code doing the same thing isn't.
@timlyo Could you post your actual code? The one you have in your question can't work, as you're checking if callable() returns a truthful value, which it never does. Also check what you're binding to it. The first argument of bind is the context, aka whatever you want this to refer to.
The logic in the question isn't important, not really sure why I added it. I've worked out my problem, and am just writing up and answer as to why it wasn't working.
0

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.

answered Jun 29, 2016 at 9:50

Comments

0

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

answered May 25, 2018 at 22:13

Comments

-1

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

answered Sep 19, 2024 at 11:54

1 Comment

Why the downvote? I answered the question and gave an alternative solution. Feedback instead of an anonymous downvote would be more constructive.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.