Reputation: 51881
How can I compare functions which were bounded before independently on context (this).
I know that Function.prototype.bind returns new function but is it possible to find out reference to the original function?
Let's say I want to implement function equalsOrigins
to do it for me:
var func1 = someFunction.bind(obj1);
var func2 = someFunction.bind(obj2);
var func3 = otherFunction.bind(obj1);
func1 === func2; // naturally returns false
equalsOrigins(func1, func2); // should return true
equalsOrigins(func1, func3); // should return false
equalsOrigins(func2, func3); // should return false
Is that possible in javascript?
Upvotes: 3
Views: 116
Reputation: 664970
Is it possible to find out reference to the original function?
No, not without access to the engine internals or a debugger. The bound function does contain a reference to the target function, but it is not accessible.
Your best bet, if the function does not throw or return an object, might be to abuse it as a constructor, and reconstruct the reference from the prototype object.
function example() { /* sane */ }
var bound = example.bind({});
Object.getPrototypeOf(new bound).constructor == example // true
Upvotes: 1