Example:
function f(a){ return a }
var g = f.bind(null);
g.toString() // chrome: function () { [native code] }
// firefox: function bound f() { [native code] }
Is there some subtle reason why g.toString() is not returning the original source code?
Of course, I could easily "fix" that by overriding Function.prototype.bind but my question is: am I doing something stupid like opening some security hole with this?
var o_bind = Function.prototype.bind;
Function.prototype.bind = function(){
var f = o_bind.apply(this, arguments);
f.orig_func = this;
return f;
}
function fsrc(f){
return f.orig_func ?
String(f.orig_func).replace(/^function/, '$& bound') :
String(f);
}
.bindreturns a new function.f.toString() != g.toString():) (thebindprobably returns a native function that when called does the actual call to the original function with the relevantthisArg)bindis to just return the followingfunction() { return f.apply(thisArg, arguments); }. A full version is not that much different but would handle the partial allocation functionality.f.toString() != g.toString()" Exactly. I usually don't expect two different functions to have the same source. Especially not a native vs a user defined function.