我正在尝试编写一个函数,该函数将另一个函数作为参数(我们可以假设另一个函数的 toString 方法将始终被覆盖并返回自定义值)并返回该函数的原始 toString 值。
function foo () {}
foo.toString = function () {
return 'abc'; }
如何恢复函数的 toString 方法,使其再次返回“function foo () {}”?
最佳答案
由于 toString
是一个原型(prototype)属性,当您删除
它时,它会恢复为默认值:
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));
关于JavaScript .toString() 默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35299159/