Asutosh
Asutosh

Reputation: 1818

How to override Function.prototype.toString

I have an requirement where I need to return function start <function code> function end to be the output of toString function get invoked on any function.

Upvotes: 0

Views: 672

Answers (1)

Ultimater
Ultimater

Reputation: 4738

This would output function start and function end wrapped around the source code of the function's source itself. If you don't want the source, simply remove it.

(function(){
    var j=Function.prototype.toString;//copy of native toString;
    Function.prototype.toString=function()
    {
        return '//function start\n'+j.call(this)+'\n//function end';
    };
})();

testy=function(a,b)
{
    return a+b;
};

alert(testy);

http://jsfiddle.net/Lx5L4qqk/

Upvotes: 1

Related Questions