Check out the edits below! I'm currently looking for a way to overload the toString method of one specific function that is generated dynamically (returned by the function). I know that I can overload the toString Function.prototype , but this will overload all toString functions of all functions, I want to avoid this.
My sample function:
var obj = { callme: function() { return function() {
So far, I have been trying to just treat a function as a regular JavaScript object.
func.toString = function() { return this(); }
This causes Function.prototype.toString still be called instead of func.toString .
Attempting to access func.prototype not possible, the prototype property is undefined, because it is a function, not an object. Overwriting toString of Function.prototype not an option; changing a func object to an object is also not possible, since it can slow down compatibility with old parts of the code.
Edit: The attempts made above obviously do not work, as I am overwriting the toString the func function, not the toString returned function. Now the best question is: is there an elegant way to overwrite toString all functions returned by func so that they "share" the same toString . (So I do not need to specify toString for each function returned.)
source share