Emil Mladenov
Emil Mladenov

Reputation: 320

JavaScript .toString() default value

I am trying to write a function that will take another function as an argument (we can assume that the other function's toString method will always be overridden ti return a custom value) and return the function's original toString value.

     function foo () {} 
     foo.toString = function () {
     return 'abc';    }

How do I revert the function's toString method so it will return "function foo () {} " again?

Upvotes: 0

Views: 201

Answers (1)

georg
georg

Reputation: 214949

Since toString is a prototype property, when you delete it, it reverts to the default:

function foo () {} 

foo.toString = function () {
     return 'abc';    
}

document.write('<pre>'+JSON.stringify(String(foo),0,3));

delete foo.toString

document.write('<pre>'+JSON.stringify(String(foo),0,3));
               

Upvotes: 3

Related Questions